2010-03-18 53 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