Actions
An action is a message emitted by an instance of a UIControl subclass (acontrol) reporting a significant user event taking place in that control (seeChapter 7). The UIControl subclasses are all simple interface objects that the user can interact with directly, like a button (UIButton), a switch (UISwitch), a segmented control (UISegmentedControl), a slider (UISlider), or a text field (UITextField).
The significant user events (control events) are listed under UIControlEvents in the Constants section of the UIControl class documentation. Different controls implement different control events: for example, a segmented control’s Value Changed event signifies that the user has tapped to select a different segment, but a button’s Touch Up Inside event signifies that the user has tapped the button. Of itself, a control event has no external effect; the control responds visually (for example, a tapped button looks tapped), but it doesn’t share the information that the event has taken place. If you want to know when a control event takes place, so that you can respond to it in your code, you must arrange for that control event to trigger an action message.
Here’s how it works. A control maintains an internal dispatch table: for each control event, there can be any number of target–action pairs, in each of which theaction is a message selector (the name of a method) and thetarget is the object to which that message is to be sent. When a control event occurs, the control consults its dispatch table, finds all the target–action pairs associated with that control event, and sends each action message to the corresponding target
There are two ways to manipulate a control’s action dispatch table: you can configure an action connection in a nib (as explained inChapter 7), or you can use code. To use code, you send the control the message addTarget:action:forControlEvents:, where the target: is an object, the action: is a selector, and the controlEvents: are designated by a bitmask
[self.button addTarget:self action:@selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];