Use of NSNotification for IOS development (76)

Source: Internet
Author: User
Tags notification center

If you want to execute methods in another class in one class, you can use notifications.
1. Create a notification object: icationicationwithname: object: Or notifwitwithname: object: userInfo:

NSNotification * notification = [NSNotification notifnotifwithname: kimagenotifloadloadfailed (connection. imageURL)
Object: self
UserInfo: [NSDictionary dictionaryWithObjectsAndKeys: error, @ "error", connection. imageURL, @ "imageURL", nil];

Note that creating your own notifications is not necessary. Instead, before creating your own notifications, use the nsicationcenter center class method postNotificationName: object: And postNotificationName: object: userInfo: to send notifications more conveniently. In this case, the default notification object is obtained using the class method of nsicationicationcenter defacenter center, so that you can send a notification to the default notification center of the program. Note: Each program has its own notification center, that is, the nsicationicationcenter object. This object uses the single-sample design mode and the defacenter center method to obtain a unique NSNotificationCenter object.

Note: The NSNotification object is unchangeable, because once created, the object cannot be changed.

2. Registration notification: addObserver: selector: name: object:

You can see that in addition to adding an observer, there is also an entry to the execution method after the notification is received, that is, the real parameter of selector. Therefore, for defensive programming, it is best to first check whether the observer has defined this method. For example, the Add observer code has

[[Nsicationcenter center defacenter center] addObserver: self
Selector: @ selector (aWindowBecameMain :)
Name: NSWindowDidBecomeMainNotification object: nil];


Here we ensure that self defines aWindowBecameMain: method. For any observer, the corresponding selector cannot have aWindowBecameMain:. You can use [observer respondsToSelector: @ selector (aWindowBecameMain :)] to check. Therefore, the complete process of adding an observer is as follows:

If ([observer respondsToSelector: @ selector (aWindowBecameMain :)]) {
[[Nsicationcenter center defacenter center] addObserver: observer selector: @ selector (aWindowBecameMain :) name: NSWindowDidBecomeMainNotification object: nil];
}


Note that addObserver: selector: name: object: not only specifies an observer, but also specifies the message sent from the notification center to the observer, as well as the name of the notification received and the specified object. Generally, you do not need to specify the name and object, but if you only specify an object, the observer will receive all notifications of this object. For example, if the name in the above Code is changed to nil, the observer will receive all the messages of the object, but cannot determine the order in which the messages will be received. If a notification name is specified, the observer receives the notification sent each time. For example, if the object in the above Code is nil, the customer object (self) will receive an NSWindowDidBecomeMainNotification notification from any object. If neither the specified object nor the name is specified, the observer will receive all messages of all objects.


3. Send a notification: postNotificationName: object: Or descrimselecdomainmainthread: withObject: waitUntilDone:

For example, a program can perform a series of conversions to a text, for example, converting an instance or RTF format to ASCII format. The conversion is processed in an object of A Class (such as the Converter class). You can add or delete the conversion during the process of searching for execution. In addition, when adding or deleting Converter operations, your program may need to notify other objects, but these Converter objects do not need to know what the notification object is and what it can do. You only need to declare two notifications, "ConverterAdded" and "ConverterRemoved", and send these two notifications when an event occurs.

When a user installs or deletes a Converter, it sends the following message to the notification center:

[[Nsicationicationcenter defacenter center]
PostNotificationName: @ "ConverterAdded" object: self];

Or

[[Nsicationicationcenter defacenter center]
PostNotificationName: @ "ConverterRemoved" object: self];

The notification center will differentiate the objects that are interested in these notifications and notify them. If you care about the notification name and object of the observer and other objects, you can put the other objects in the optional Dictionary of the notification, or use the postNotificationName: object method: userInfo :.

When the parameter mselecstmmainthread: withObject: waitUntilDone: is used to directly call the NSNotification method postNotification, while the posticationicationname and object parameters can be placed in the real parameters of withObject. For example:

[[Nsicationicationcenter defacenter center] performSelectorOnMainThread: @ selector (postNotification :) withObject: notification waitUntilDone: YES]; // note that the notification here is a custom notification object, it can be defined as NSNotification * notification = [NSNotification notificationWithName: @ "ConverterAdded" object: self]; // The function is the same as the above.


4. Removal notification: removeObserver: And removeObserver: name: object:

Among them, removeObserver: is to delete all the entries of the observer in the scheduling table saved by the notification center, while removeObserver: name: object: is to delete an entry of the observer in the scheduling table saved by the notification center.


This is relatively simple, just call this method. For example:

[[Nsicationcenter center defacenter center] removeObserver: observer name: nil object: self];


Note that icationicationobserver is the observer to be deleted and cannot be set to nil.

PS: The scheduling table saved in the notification center. The scheduling table of the notification center specifies some Notification sets for some observers. A notification set is a subset of notifications sent by the notification center. The entries for each table include:

Notification observer (required), notification name, and notification sender.

Indicates four types of the call table entry for the specified notification in the notification set:


Scheduling table for four types of observers

 
 

 

Finally, the sequence in which the observer receives the notification is not defined. At the same time, the notification may be the same as the observed object. The notification center synchronously forwards the notification to the observer, that is, postNotification: The method returns the value until the notification is received and processed. To send notifications asynchronously, you can use NSNotificationQueue. In multi-thread programming, a notification is generally forwarded in the thread that sends the notification, but it may also be not forwarded in the same thread.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.