Set Action Listener Programmatically in IOS

Tahlil picture Tahlil · Dec 2, 2012 · Viewed 9.7k times · Source

Hi I have created a button programmatically. I will add this button to the navigation bar. Now I want to add a Touch Up Inside action listener to it. How do I do it? Thanks.

Answer

Abizern picture Abizern · Dec 2, 2012

A UIButton is a subclass of UIControl.

All you need to do after creating an button is to set the target and action of the button. i.e.

// Create your button:
UIButton *button = // However you create your button

// Set the target, action and event for the button
[button addTarget:// the object that implements the action method, or nil if you want it to propagate up the responder chain.
           action:// A selector for the method
 forControlEvents:UIControlEventTouchUpInside];