Use of IOS nsnotification

Source: Internet
Author: User
Tags notification center

http://blog.csdn.net/dqjyong/article/details/7678108

If you want to perform a method in another class in one class, you can use the notification
1. Create a Notification object: Use Notificationwithname:object: or notificationWithName:object:userInfo:

    nsnotification* notification = [Nsnotification notificationwithname: Kimagenotificationloadfailed (connection.imageurl)
                                                                    object:self
                                                                 userInfo : [Nsdictionary dictionarywithobjectsandkeys:error,@ "error", connection.imageurl,@ "ImageURL", Nil]];

It is important to note that it is not necessary to create your own notifications. Instead, before creating your own notifications, take the Nsnotificationcenter class method Postnotificationname:object: and PostNotificationName:object:userInfo: More convenient to give notice. In this case, it is common to use the Nsnotificationcenter class method Defaultcenter to get the default notification object so that you can send notifications to the program's default notification hubs. Note: Each program has its own notification hub, which is the Nsnotificationcenter object. The object uses a singleton design pattern, and a unique Nsnotificationcenter object can be obtained by using the Defaultcenter method.

Note: The Nsnotification object is immutable, because once created, the object cannot be changed.

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

You can see that in addition to adding observers, there is an entry for the execution method after receiving the notification, that is, the selector argument. So for defensive programming, it's a good idea to check if the Observer defines the method first. For example, adding the Observer code has

[[Nsnotificationcenter Defaultcenter] Addobserver:self
Selector: @selector (awindowbecamemain:)
Name:nswindowdidbecomemainnotification Object:nil];

This ensures that self defines the Awindowbecamemain: method. And for an arbitrary observer observer, there is no guarantee that the corresponding selector have Awindowbecamemain:, can be used [observer Respondstoselector: @selector ( Awindowbecamemain:)]] to check. So the complete addition of the observer process is:

if ([Observer Respondstoselector: @selector (awindowbecamemain:)]) {
[[Nsnotificationcenter Defaultcenter] addobserver:observer selector: @selector (awindowbecamemain:) Name: Nswindowdidbecomemainnotification Object:nil];
}

Note that AddObserver:selector:name:object: Not only specifies an observer, specifies the message sent to the observer by the notification hub, but also the name of the receiving notification, as well as the specified object. In general, you do not need to specify name and object, but if you specify only one object, the observer will receive all notifications for that object. For example, to change the name in the above code to nil, the observer will receive all the messages for the object, but the order of receiving these messages is not determined. If you specify a notification name, the observer receives a notification each time it is issued. For example, if object is nil in the code above, then the client object (self) will receive an nswindowdidbecomemainnotification notification from any object. If neither specified object is specified nor name is specified, then the observer will receive all messages for all objects.

3. Send notification: postnotificationname:object: or PerformSelectorOnMainThread:withObject:waitUntilDone:

For example, a program can implement a series of transformations of a text, such as an instance, RTF format converted to ASCII format. Conversions are handled in objects of a class (such as the converter Class), which can be added to or removed from the search execution. And when adding or removing converter operations, your program may need to notify other objects, but these converter objects do not need to know what the notified object is and what to do. You only need to declare two notifications, "converteradded" and "converterremoved", and issue these two notifications when an event occurs.

When a user installs or deletes a converter, it sends the following message to the notification hub:

[[Nsnotificationcenter Defaultcenter]
postnotificationname:@ "converteradded" object:self];

or a

[[Nsnotificationcenter Defaultcenter]
postnotificationname:@ "converterremoved" object:self];

Notification hubs will differentiate their objects from being interested in these notifications and notifying them. If, in addition to caring for the observer's notification name and the observed object, but also for other objects, then put the outside of the object in the optional dictionary of the notification, or the method PostNotificationName:object:userInfo:.

and using perf OrmSelectorOnMainThread:withObject:waitUntilDone: It is the method postnotification that calls the nsnotification directly, The Postnotificationname and the object arguments can be placed in the Withobject argument. For example:

[[Nsnotificationcenter Defaultcenter] Performselectoronmainthread: @selector (postnotification:) WithObject: notification waituntildone:yes];//Note here that the notification is a custom notification object that can be defined as nsnotification* notification = [Nsnotification notificationwithname:@ "converteradded" object:self];//then its effect is consistent with the above

4. Remove notification: Removeobserver: and RemoveObserver:name:object:

Where Removeobserver: is the DELETE Notification Center saves the schedule table for all the portals of an observer, and RemoveObserver:name:object: is to delete a portal that matches the observer in the dispatch table saved by the notification center.

This is relatively straightforward, just call the method on the line. For example:

[[Nsnotificationcenter Defaultcenter] removeobserver:observer name:nil object:self];

Note that the parameter notificationobserver is the observer to be deleted and must not be nil.

PS: Here is a brief statement of the dispatch table saved by the notification center. The dispatch table for notification hubs is a set of notifications that are specified for some observers. A notification set is a subset of notifications sent by notification hubs. The entry for each table contains:

Notifies the Observer (required), the name of the notification, and the sender of the notification.

Represents the four types of call table entries for notifications specified in the notification set:

A dispatch table representing four types of observers

Finally, remind the Observer that the order in which the notifications were received is undefined. It is also possible to notify the same object that is emitted and observed. Notification hubs synchronously forwards notifications to observers, that is, Postnotification: The method does not return a value until the notification is received and processed. To send notifications asynchronously, you can use Nsnotificationqueue. In multithreaded programming, notifications are generally forwarded in the same thread as a notification, but may not be forwarded in the same thread.

Use of IOS nsnotification

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.