How to Use the Target-Action Pattern in Your iOS App
One requirement of an iOS app user interface is being able to deal with situations where you want to connect a button to your code so that when a user taps the button, something happens. This requirement involves using the Target-Action pattern, which is one of the key design patterns in iOS programming.
You use the Target-Action pattern to let your app know that it should do something when prompted. A user might tap a button or enter some text, for example, and the app must respond in some way. The control sends a message (the Action message) that you specify to the target (the receiving object, which is usually a view controller object) that you have selected to handle that particular action.
When a user acts on the control by, say, tapping a button, the iPhone or iPad generates an event. The event triggering a particular action message can be anything, just as the object sending the message can be any object. A gesture-recognizer object might send an action message to another object when it recognizes its gesture. However, the Target-Action pattern is usually found with controls such as buttons and sliders.
The event as such probably doesn’t tell you much, but Target-Action provides a way to send an app-specific instruction to the appropriate object.
If you wanted to develop an app that could start a car from an iOS device (not a bad idea for those who live in a place like Hibbing, Minnesota in winter), you could display two buttons, Start and Heater. You could use Interface Builder to specify that when the user taps Start, the target is the CarController object and the method to invoke is ignition.
The Target-Action mechanism enables you to create a control object and tell it not only which object you want handling the event but also the message to send. For example, if the user taps a Ring Bell button onscreen, you want to send a Ring Bell message to the view controller.
But if the Wave Flag button on the same screen is touched, you want to be able to send the Wave Flag message to the same view controller. If you couldn’t specify the message, all buttons would have to send the same message.
It would then make the coding more difficult and more complex because you would have to identify which button sent the message and what to do in response. It would also make changing the user interface more work and more prone to errors.
You set a control’s action and target using Interface Builder. You get to specify what method — in which object — should respond to a control without having to write any code.
Action methods have a certain signature (format, in other words):
The type qualifier IBAction, which is used in place of the void return type, flags the declared method as an action so that Interface Builder is aware of it. And just as with outlets, you can actually make the connections in the Interface Builder editor, and Xcode will generate the necessary code for you.
The sender parameter is the control object sending the action message. When responding to an action message, you may query the sender to get more information about the context of the event triggering the action message.
You can set the action and target of a control object programmatically or in Interface Builder. Setting these properties effectively connects the control and its target via the action. If you connect a control and its target in Interface Builder, the connection is archived in a nib file. When an app later loads the nib file, the connection is restored.
IBAction is like IBOutlet— it does nothing in the code but rather is a tag used by Interface Builder.