Detecting UIButton touches on a subview

2019-07-29 19:07发布

问题:

I have added a subview which contains a UIButton control. This subview is added to its superview programatically. I need to perform an action when this button is tapped, but since it's contained within its own subview, I can't hook an IBAction up to the view controller in order to push another view controller.

Is there an easy way to detect that the button is tapped and call a method within its super view?

回答1:

You can do everything programmatically:

[buttonName addTarget:self action:@selector(methodName:) forControlEvents:UIControlEventTouchUpInside];

and then create your method:

- (void)methodName:(id)sender
{
     // Do something.
}

This is all you have to code.