2014-05-11 63 views
0

我有一個ViewController,它擁有六個按鈕。每個按鈕都有.tag s 1到6,並且都連接到使用.tag找出該做什麼的相同IBAction。在4英寸設備上一切都很好。爲什麼我不能與我的按鈕進行交互?

然而,在3.5英寸的設備中,我需要重新安排按鈕以使其合適。我試圖只使用[self.view viewWithTag: tag]來檢索按鈕,並更改檢索到的按鈕的.frame,但這似乎沒有做任何事情。因此,我做了以下操作:

-(void) rearrangeButtons 
{ 
    int space = 7; // space between the first button and the left side of the screen as well as between individual buttons 

    for(int i = 0; i < 6; i++) { 
     // Get the button and remove from superview: 
     UIButton *button = (UIButton *)[self.view viewWithTag: i+1]; 
     [button removeFromSuperview]; 

     // Resize its width and height: 
     UIButton *newButton = [UIButton buttonWithType: UIButtonTypeCustom]; 
     newButton.frame = CGRectMake(space + (i*(button.frame.size.width/2)) + (i*space), 170, button.frame.size.width/2, button.frame.size.height/2); 
     newButton.tag = button.tag; 

     //[newButton addTarget:self action:@selector(highlightButtonWithTag:) forControlEvents:UIControlEventTouchDown]; 
     [newButton targetForAction:@selector(highlightButtonWithTag:) withSender:newButton]; 

     [newButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"icon%i", i+1]] forState:UIControlStateNormal]; 

     [self.view addSubview: newButton]; 
    } 
} 

這將刪除按鈕並將它們放在新按鈕中。安排他們工作正常。然而,我想調用的選擇器沒有被調用,我似乎無法操作按鈕的屬性,因爲我可以使用之前做過的按鈕(即使這也完全基於它們的標籤)。

- (IBAction) selectIcon:(UIButton *)sender 
{ 
    NSLog(@"called"); 
    // "Un"highlight previous button 
    UIButton *prevButton = (UIButton *)[self.view viewWithTag: self.selectedIcon]; 
    prevButton.highlighted = NO; 

    // Highlight tapped button: 
    self.selectedIcon = sender.tag; 
    [self performSelector:@selector(highlightButtonWithTag:) withObject:@(self.selectedIcon) afterDelay:0]; 
} 

我是什麼在這裏失蹤?謝謝!

+0

你好弗洛裏安您正在craeting新鍵 「 //調整其寬度和高度: 的UIButton * newButton = [UIButton的buttonWithType:UIButtonTypeCustom];」對於改變幀你只需​​寫出button.frame = cgrectmake(放幀)。無需創建另一個按鈕 –

+0

嘿 - 謝謝你的回覆!不過,我不太清楚你的代碼與我發佈的代碼有什麼不同。我使用'buttonWithType:'來創建一個'UIButton',然後像你所建議的那樣設置它的'.frame'屬性。或者我誤解了你? – Florian

回答

0

而不是創建一個新的按鈕實例中,嘗試使用與該標籤定義的現有之一。在這種情況下,你不必添加和從超級視圖中刪除按鈕,只需重新使用它們並在這些按鈕上設置框架就可以了

-(void) rearrangeButtons 
{ 
    int space = 7; // space between the first button and the left side of the screen as well as between individual buttons 

    for(int i = 0; i < 6; i++) { 
     UIButton *newButton = (UIButton *)[self.view viewWithTag: i+1] ; 
     if(newButton == nil) { 
      newButton = [UIButton buttonWithType: UIButtonTypeCustom]; 
      newButton.tag = i+1; 
      [self.view addSubview: newButton]; 
     } 
     newButton.frame = CGRectMake(20, i*(i+1) *20, 50, 50); 
     [newButton addTarget:self action:@selector(selectIcon:) forControlEvents:UIControlEventTouchDown]; 
     // [newButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"icon%i", i+1]] forState:UIControlStateNormal]; 
     [newButton setBackgroundColor:[UIColor redColor]]; 
    } 
} 
- (IBAction) selectIcon:(UIButton *)sender 
{ 
    NSLog(@"called"); 
    // "Un"highlight previous button 
    UIButton *prevButton = (UIButton *)[self.view viewWithTag: self.selectedIcon]; 
    prevButton.highlighted = NO; 

    // Highlight tapped button: 
    self.selectedIcon = sender.tag; 
    [self highlightButtonWithTag:self.selectedIcon]; 
} 

-(void)highlightButtonWithTag:(NSInteger) selectedIcon 
{ 
    NSLog(@"highlightButton at position: %ld", (long)selectedIcon); 
} 
+0

感謝您的回覆。 不幸的是,這似乎並不奏效。正如我在我的問題中所說的,我試圖調整使用'viewWithTag:'檢索到的'UIButton'的'.frame'屬性,但是根本不改變按鈕。 我再次使用你的代碼嘗試 - 但仍然沒有運氣。 – Florian

+0

你還在使用代碼在superview中添加/刪除按鈕嗎?當newButton爲空時,你只需要做一次。 –

+0

沒有。我只是用我發佈的代碼的等價物替換了我的for循環中的所有內容。 – Florian

0

爲什麼不使用addTarget:action:forControlEvents:?

[newButton addTarget:self 
       action:@selector(highlightButtonWithTag:) 
    forControlEvents:UIControlEventTouchUpInside]; 
相關問題