How to intercept button taps?
How I tell which button is tapped the user in a view? Is there a "aspect" way of capturing the events when a UIButton is clicked?
Basically, without any ad开发者_如何转开发ditonal code, I am able to capture all button clicks.
Connect your button to an action in your view controller. When the user taps the button, it will execute the action.
see the code below
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];
So all the click event on myButton
will be received in buttonClicked
method,
Implement buttonClicked
method ,
-(void) buttonClicked:(id) sender
{
UIButton* myButton = (UIButton*) sender;
........
.......
}
Not sure about views, but in a view controller, you can specify a method with the pattern as below:
- (IBAction)mybuttontapped:(id)sender;
You will need to connect your button in the interface builder for the desired event to the handler. Works for me.
精彩评论