Search code examples
objective-cuibuttonsubviewibaction

Detecting UIButton touches on a subview


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?


Solution

  • 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.