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

Button 向子视图中的按钮添加操作,button,uiview,action,Button,Uiview,Action,在我的应用程序中,我调用UIView,UIView就像我应用程序中的设置屏幕,我在UIView中有按钮,我的问题是如何向添加到UIView子视图的按钮iv添加操作?谢谢,假设您正在为设置UIView在视图控制器中编写代码,UIView已正确绑定到控制器的视图属性,并且您已使用变量按钮引用了其中一个按钮,以下是您将要编写的内容: - (void)viewDidLoad { [super viewDidLoad]; [button addTarget:self

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

假设您正在为设置UIView在视图控制器中编写代码,UIView已正确绑定到控制器的
视图
属性,并且您已使用变量
按钮
引用了其中一个按钮,以下是您将要编写的内容:

- (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.
}

但是,在定义了上面的
按钮pressed
按钮pressed:
方法后,在
.xib
文件中单击按钮,转到检查器中的第二个选项卡,而不是在Interface Builder中调用
addTarget:action:forControlEvents:
(应该说是按钮连接),然后单击并将“内部润色”事件拖动到文件所有者,然后选择“buttonPressed”从列表中。

假设您正在为设置UIView在视图控制器中编写代码,UIView将正确绑定到控制器的
视图
属性,并且您使用变量
按钮
引用了其中一个按钮,下面是您将要编写的内容:

- (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.
}
但是,在定义了上面的
按钮pressed
按钮pressed:
方法后,在
.xib
文件中单击按钮,转到检查器中的第二个选项卡,而不是在Interface Builder中调用
addTarget:action:forControlEvents:
(应该说是按钮连接),然后单击并将“内部润色”事件拖动到文件的所有者,然后从列表中选择“buttonPressed”