Understanding and application of objective-c commission

Delegate (delegate):

Delegation is a design pattern. Delegation means that an object in the program replaces another object to complete certain tasks, or coordinates with another object to complete certain tasks. Among them, the delegator holds a reference to the delegate and sends a message to the delegate at an appropriate time. Through this message, the delegator notifies the delegator that he is about to process or has processed a certain event. The delegator can update its own or the user interface in the program or the state of other objects in response to the message; and can also inform the delegator of his handling of the event through the return value, so that the delegator can decide how to take it Further actions.

Delegation and cocoa framework (delegation and cocoa framework)


In the Cocoa framework, the delegator is usually an object of the framework class, and the delegate is usually a custom controller object. In an environment that requires memory management, the delegator will hold a weak reference to the delegator; and in a garbage collection environment, the delegator holds a strong reference to the delegatee. In the basic libraries, UIKit, APPKit, and other Cocoa libraries and Cocoa frameworks, there are a large number of commissioned examples.

 

An example of a delegate is an object of the NSWindow class in the AppKit framework as the delegate. NSWindow declares a protocol (Protocol), which has a method windowShouldClose:. When the user clicks the close button on the form, the form object will send a windowShouldClose: message to its delegate (delegator), asking whether to confirm to close the form. The delegate of the form can control the behavior of the form object by returning a Boolean value.

As shown below:





Form object (delegator) delegate (delegator)


Delegation and Notifications


The delegates of most classes in the Cocoa framework are automatically registered as observers of notifications sent by the delegate. The delegate only needs to implement the notification method declared in the framework class to receive the specified notification message. As in the previous example, the form object will send an NSWindowsWillCloseNotification message to all observers, but will only send a windowShouldClose: message to its delegate.


Data Source (DataSource)


The data source is almost the same as when commissioned. The difference lies in their relationship with the client. The data source is delegated to control the data, not to process the user interface. The delegate, usually a view object, such as a table view, will hold a reference to its data source and request the data that needs to be displayed as needed. The data source, similar to the delegation, must comply with a certain agreement and implement the minimum method set required in the agreement. The data source is responsible for the memory management of the data model objects of its principal view.


Notification


Notification is a message sent to one or more observers to notify them that an event has occurred in the program. The notification mechanism in Cocoa follows a broadcast model. It is a way for the initiator or handler of an event in a program to communicate with other objects who want to know the event. The receiver of the message, that is, the observer, responds to the event to change its UI, behavior, or state. It is not necessary for the person sending the notification to know who these observers are. Therefore, the notification is a mechanism that can achieve efficient collaboration in the program while maintaining high cohesion. It reduces the strong dependence between objects in the program (this dependence will greatly reduce the reusability of the code in the program). Many classes in the basic library, AppKit, and other Objective-C frameworks define notifications so that we can register as notification observers.

 

通知机制的核心就是一个进程中单一实例的对象,被叫做通知中心(NSNotificationCenter)。当一个对象发布一个通知时,通知会先被发布到通知中心。通知中心的作用相当于是交流所,作为通知的广播中心。程序中其他需要感知该事件的对象通过向通知中心注册就可以达到在事件发生时被通知中心及时通知到得目的。通知中心是可以以同步的方式向其观察者发送通知,也是可以通过使用通知队列(NSNotificationQueue)来异步地发送通知。

 





用于表示通知的对象


一个通知是用NSNotification类的一个对象来表示的。表示通知的对象中含有用于表示该通知的信息的字段:通知的名称,发布通知的对象以及一个用于表示其他补充信息的字典。这个字典被称为是userInfo字典。当通知被发送给对其感兴趣的观察者时,该表示通知的对象会被作为参数传入到处理该通知的方法中。


观察指定的通知


实现观察某个通知的时候,我们先要获取NSNotificationCenter的单实例对象并向其发送addObserver:selector:name:object:消息。通常情况下,这种注册的行为在应用程序启动后就要进行。addObserver:selector:name:object:方法的第二个参数是一个选择器,该选择器指定的是处理指定通知的具体方法。方法的原型必需如下:

-(void)myNotificationHandler:(NSNotification*)notif;

在该方法中,我们可以提取通知中的相关信息来帮助我们处理数据,特别是userInfo中的数据(如果userInfo存在的话)。


发布通知


通常在发布通知之前,我们都需要定义一个全局的字符串常量来作为通知的名称。传统的做法是采用和应用程序相关的两个或者三个字母作为通知名称的前缀,例如:

NSString & AMMyNotification = @”AMMyNotification”;

The notification is published by sending a postNotificationName:object:userInfo: (or similar) message to the single-instance object of NSNotificationCenter. Before sending a notification to the notification center, this method first creates an object that represents the notification.


Original delegation mechanism: https://developer.apple.com/library/mac/#documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html

Original notification mechanism: https://developer.apple.com/library/mac/#documentation/General/Conceptual/DevPedia-CocoaCore/Notification.html


Guess you like

Origin blog.csdn.net/qq_27740983/article/details/50143985