如何拦截按钮水龙头?

时间:2011-03-29 03:40:12

标签: iphone

我如何判断在视图中点击了哪个按钮? 单击UIButton时是否有“方面”方法捕获事件?

基本上,没有任何附加代码,我可以捕获所有按钮点击。

3 个答案:

答案 0 :(得分:0)

不确定视图,但在视图控制器中,您可以使用以下模式指定方法:

- (IBAction)mybuttontapped:(id)sender;

您需要将界面构建器中的按钮连接到处理程序以获取所需事件。适合我。

答案 1 :(得分:0)

将按钮连接到视图控制器中的操作。当用户点击按钮时,它将执行操作。

答案 2 :(得分:0)

请参阅以下代码

 UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    myButton.frame = CGRectMake(20, 20, 200, 44); // position in the parent view and set the size of the button
    [myButton setTitle:@"Click Me!" forState:UIControlStateNormal];
    // add targets and actions
    [myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    // add to a view
    [superView addSubview:myButton];

因此myButton上的所有点击事件都会以buttonClicked方式收到, 实施buttonClicked方法,

-(void) buttonClicked:(id) sender
{
   UIButton* myButton = (UIButton*) sender;

   ........

   .......
}
相关问题