2010-03-18 59 views
2

我需要更改UIButtons矩陣上的圖像,而我所知道的唯一解決按鈕問題的是標籤。但我找不到實際使用此標識符的方法。 這些按鈕是在viewDidLoad期間以編程方式創建的。如何使用標籤訪問UIButton並更改其圖像?

下面是用於創建按鈕的代碼:

#define N_ROWS 4 
#define N_COLS 3 

    int N_IMG = 0; 
    for (int a = 0; a < N_COLS; a++) { 
     for (int j = 0; j < N_ROWS; j++) { 


      UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
      aButton.frame = CGRectMake(a * 65.0 + 25, j * 65.0 + 15, 10.0, 10.0); 
      aButton.tag = j + a * N_ROWS + 1; 
      [aButton setBackgroundColor:[UIColor redColor]]; 

      N_IMG = N_IMG++; 
      [self.view addSubview:aButton]; 
      number_sorted = 1; 

     } 
    } 

這裏是用於設置圖像的代碼:

- (IBAction)set_image:(id)sender { 

    #define N_ROWS 4 
    #define N_COLS 3 

     int N_IMG = 0; 
     for (int a = 0; a < N_COLS; a++) { 
      for (int j = 0; j < N_ROWS; j++) { 
       uibutton aButton.tag == (j + a * N_ROWS + 1) 
       setImage:[UIImage imageNamed:[puzzles objectAtIndex:N_IMG]] 
          forState:UIControlStateNormal]; 
      N_IMG = N_IMG++; 

      } 
     } 
    } 

這是代碼,其中truble開始: 的UIButton aButton.tag ==(j + a * N_ROWS + 1)

我可以設置這項功能嗎?

回答

4

我真的不明白你想做什麼,但爲什麼你不能存儲你的UIButton對象(即在一個NSArray對象),所以你可以稍後訪問它們(在你的第二個循環中)?

8

答案很簡單:

RE:「如何使用標籤訪問一個UIButton ......」

UIButton *tmpButton = (UIButton *)[self.view viewWithTag:tmpTag]; 

RE: 「......和改變自己的形象」

[tmpButton setImage:[UIImage imageNamed:@"MyGreatImage.png"] forState:UIControlStateNormal]; 

龍答:

RE:* 「這是代碼,其中truble開始:UIButton的aButton.tag ==(J + A * N_ROWS + 1)」 *

是的,我可以看到麻煩。您正在使用的線路爲設置一個按鈕的標記,而不是得到按鈕標記。

要想從已知標籤上的按鈕做到這一點:

// define the tag index 
int tmpTag = 123;//replace "123" with your own logic, i.e. (j + a * N_ROWS + 1) 

// get the button with the given tag 
UIButton *tmpButton = (UIButton *)[self.view viewWithTag:tmpTag]; 

// assign the image 
tmpImage = [UIImage imageNamed:@"MyGreatImage.png"]; 
[tmpButton setImage:tmpImage forState:UIControlStateNormal]; 

獎金代碼:在這個階段,你也可以添加或刪除與您的按鈕相關的動作。

//Remove all actions associated the button 
[aButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents]; 

//Assign a new button action: using the exact selector name ("myMethodName") 
[aButton addTarget:self action:@selector(myMethodName:) forControlEvents:UIControlEventTouchUpInside]; 

//Assign a new button action: using a calculated selector name 
//(suppose I have a bunch of methods with the prefix "myNumberedMethodName_" followed by an index. 
int tmpSomeNumber = 12; 
SEL tmpSelector = NSSelectorFromString ([NSString stringWithFormat:@"myNumberedMethodName_%i:",tmpSomeNumber); 
// don't forget to include the ":" symbol in the selector name 
[aButton addTarget:self action:tmpSelector forControlEvents:UIControlEventTouchUpInside]; 

注意:「viewWithTag」通常會返回一個View對象。按鈕是一種特殊類型的視圖,具有諸如圖像等特殊屬性。因此,爲了讓它返回一個Button對象而不是更通用的View對象,我們使用(UIButton *)在定義中將其初始化爲Button。

但是,如果你想要的只是改變按鈕的不透明度,那麼你不必把它作爲一個按鈕。您可以簡單地將它初始化爲通用視圖:

// set opacity of a button 
float tmpOpacity = 0.5;//half-visible 
UIView *tmpView = [self.view viewWithTag:tmpTag];//get the view object associated with button's tag (remember, a button IS a view) 
[[tmpView layer] setOpacity:tmpOpacity]; 

另請參閱Button with Tag