从另一个按钮更改一个按钮的标题IBAction

从另一个按钮更改一个按钮的标题IBAction

问题描述:

有没有办法通过编程方式创建UIbutton时从另一个UIbuttons IBAction方法访问UIbutton?我能够通过为每个人创建一个IBOutlet并以这种方式引用它们来使用接口构建器来完成此任务,但是我需要以编程方式创建按钮,并且似乎无法弄清楚这是否可能?从另一个按钮更改一个按钮的标题IBAction

谢谢!

当您创建按钮时,将它们分配给控制器中的ivars。然后,您的IBAction方法可以根据需要访问这些ivars中的任何一个。

例如,在您的视图控制器头,你可能有:

@interface MyViewController : UIViewController 

@property (nonatomic, retain) UIButton *button1; 
@property (nonatomic, retain) UIButton *button2; 

@end 

在你实现你创建你的按钮是这样的:

UIButton *b = [[UIButton alloc] init...]; 
...more setup... 
self.button1 = b; 
[b release]; 

... 

然后在您的IBAction为方法,你可能会做像这样:

-(IBAction)buttonPress:(UIButton *)b 
{ 
    if (b == self.button1) 
     // do something to button2 
    else 
     // do something to button1 
} 

有意义吗? (这是全部从内存编码,未经测试,fwiw。)

+0

非常感谢,这工作完美。 – user1195485 2012-02-07 21:49:08