01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

172 CHAPTER 8 Dynamic typing and runtime type information<br />

performSelector:withObject:withObject:. All of these methods take a selector indicating<br />

the message to send and map directly to the messaging function. For example:<br />

[house performSelector:@selector(setAddress:)<br />

withObject:@"13 Adamson Crescent"];<br />

This line is equivalent to<br />

[house setAddress:@"13 Adamson Crescent"];<br />

The power of these methods comes from the fact that you can vary the message being<br />

sent at runtime just as easily as it’s possible to vary the object that receives the message. A<br />

variable can be used in place of the method selector, unlike in a traditional message<br />

send, which accepts only a constant in this location. In other words, the following is valid:<br />

id obj = get_object_from_somewhere();<br />

SEL msg = get_selector_from_somewhere();<br />

id argument = get_argument_from_somewhere();<br />

[obj performSelector:msg withObject:argument];<br />

But this isn’t valid:<br />

id obj = get_object_from_somewhere();<br />

SEL msg = get_selector_from_somewhere();<br />

id argument = get_argument_from_somewhere();<br />

[obj msg:argument];<br />

In this example, the receiver of the message (obj) is chosen at runtime, and the message<br />

it’s sent is also determined at runtime. This leads to a lot of flexibility in the<br />

design of an application; with functions such as NSSelectorFromString, discussed<br />

previously, it’s even possible to use selectors that exist only at runtime.<br />

The Target-Action design pattern<br />

The UIKit framework makes good use of the ability to programmatically vary both the<br />

receiver and message sent via a message send. UIView objects such as UISlider<br />

and UIButton interpret events from hardware such as the touch screen or Bluetoothconnected<br />

keyboard and convert these events into application-specific actions.<br />

For instance, when the user taps on a button, a UIButton object sends a message<br />

instructing the application that it should do something in response to the button press.<br />

A UIButton object is initialized with details on which message to send and where to<br />

send it. This is a common design pattern in UIKit called the Target-Action design pattern.<br />

As an example, you could request that a button send the buttonPressed: message<br />

to the myObject object when the user lifts their finger off the screen with the following<br />

initialization:<br />

[btn addTarget:myObject<br />

action:@selector(buttonPressed:)<br />

forControlEvents:UIControlEventTouchUpInside];<br />

The UIButton class sends the message using NSObject’s performSelector: with-<br />

Object: method, just as we discussed in this section. If <strong>Objective</strong>-C didn’t allow the<br />

message name or target to be varied programmatically, UIButton objects would all have<br />

to send the same message, and the name would be frozen in UIButton’s source code.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!