Target-action is a design pattern in which an object holds the information necessary to send a message to another object when an event occurs. The stored information consists of two items of data: an action selector, which identifies the method to be invoked, and a target, which is the object to receive the message.
The target-action mechanism enables a control object—that is, an object such as a button, slider, or text field—to send a message to another object that can interpret the message and handle it as an application-specific instruction. The receiving object, or the target, is usually a custom controller object.
target-action在Cocoa中是专门用于UI对象与非UI对象之间的通信的。
target-action in AppKit
使用方法如下:
1. 在自定义类的头文件中声明一个使用了IBAction 描述的方法(只有使用IBAction描述的方法,并且符合特定规范的方法[有唯一参数,类型为id]才会在IB中被列出);
2. 在Interface Builder中,Control-Click 自定义类的object,并将使用IBAction描述的方法与UI中某个Control关联;
当然也可以使用编程的方式实现(假定UI上有个一个NSButton,在custom class中设置button为该NSButton对应的outlet,并在UI中做绑定)
-(void) awakeFromNib { [button setTarget:self]; [button setAction:@selector(handleClick:)]; }
在AppKit中,UI对象的继承关系如下:
从这个图中,我们可以看到,对于Target-Action的支持是从NSControl开始的。但NSControl并不是唯一的根,下面是NSCell一系的:
也就是说NSCell也可以支持Target-Action. Although a control has the initial responsibility for sending an action message to its target, it rarely carries the information needed to send the message. For this, it usually relies on its cell or cells. 关于Control-Cell需要另行介绍。
target-action in UIKit
The UIKit framework also declares and implements a suite of control classes; the control classes in this framework inherit from the UIControl class, which defines most of the target-action mechanism for iOS. However there are some fundamental differences in how the AppKit and UIKit frameworks implement target-action. One of these differences is that UIKit does not have any true cell classes. Controls in UIKit do not rely upon their cells for target and action information.
A larger difference in how the two frameworks implement target-action lies in the nature of the event model. In the AppKit framework, the user typically uses a mouse and keyboard to register events for handling by the system. These events—such as clicking on a button—are limited and discrete. Consequently, a control object in AppKit usually recognizes a single physical event as the trigger for the action it sends to its target. (In the case of buttons, this is a mouse-up event.) In iOS, the user’s fingers are what originate events instead of mouse clicks, mouse drags, or physical keystrokes. There can be more than one finger touching an object on the screen at one time, and these touches can even be going in different directions.
事实上,这一条不足够解释为什么MacOSX和iOS使用不同的模型。mcd
To account for this multitouch event model, UIKit declares a set of control-event constants in UIControl.h that specify various physical gestures that users can make on controls, such as lifting a finger from a control, dragging a finger into a control, and touching down within a text field. You can configure a control object so that it responds to one or more of these touch events by sending an action message to a target. Many of the control classes in UIKit are implemented to generate certain control events; for example, instances of the UISlider class generate a UIControlEventValueChanged control event, which you can use to send an action message to a target object.
You set up a control so that it sends an action message to a target object by associating both target and action with one or more control events. To do this, send addTarget:action:forControlEvents: to the control for each target-action pair you want to specify. When the user touches the control in a designated fashion, the control forwards the action message to the global UIApplication object in a sendAction:to:from:forEvent: message. As in AppKit, the global application object is the centralized dispatch point for action messages. If the control specifies a nil target for an action message, the application queries objects in the responder chain until it finds one that is willing to handle the action message—that is, one implementing a method corresponding to the action selector.
In contrast to the AppKit framework, where an action method may have only one or perhaps two valid signatures, the UIKit framework allows three different forms of action selector:
- (void)action
- (void)action:(id)sender
- (void)action:(id)sender forEvent:(UIEvent *)event
To learn more about the target-action mechanism in UIKit, read UIControl Class Reference.
outlet
outlet是一个Property,Interface Builder 会读取类的.h文件并将所有可以成为outlet的Property列出来。那什么样的Property可以成为outlet呢?Xcode4只接受使用IBOutlet标记的Property为outlet。在awakeFromNib被调用之前,所有关联的outlet会被正确的设置。