向子视图中的按钮添加操作

时间:2011-03-02 01:49:04

标签: button uiview action

在我的应用程序中我调用UIView并且UIView就像我的应用程序中的设置屏幕,我在UIView中有按钮,我的问题是如何添加动作到添加到UIViews子视图的按钮?感谢,

1 个答案:

答案 0 :(得分:1)

假设您正在为您的设置UIView在视图控制器中编写代码,UIView已正确绑定到控制器的view属性,并且您已使用变量{{1}引用其中一个按钮这是你要写的:

button

编写该方法的另一种方法是传入指向实际按下的按钮的指针,如下所示:

- (void)viewDidLoad {
    [super viewDidLoad];
    [button addTarget:self
               action:@selector(buttonPressed)
     forControlEvents:UIControlEventTouchUpInside];
}

- (void)buttonPressed
{
    // do things here in response to the button being pressed
}

在定义上面的- (void)viewDidLoad { [super viewDidLoad]; [button addTarget:self action:@selector(buttonPressed:) // note the extra colon here forControlEvents:UIControlEventTouchUpInside]; } - (void)buttonPressed:(id)sender { UIButton *buttonWhichWasPressed = (UIButton *)sender; // now you can do things like hide the button, change its text, etc. } addTarget:action:forControlEvents:方法后,在Interface Builder中(你应该这样做),而不是调用buttonPressed单击按钮后,带有按钮的buttonPressed:文件将转到检查器中的第二个选项卡(它应该显示按钮连接),然后单击并将Touch Up Inside事件拖动到文件所有者,并从中选择“buttonPressed”列表。

相关问题