iOS Development UI article-Notification Center (Nsnotificationcenter)

Source: Internet
Author: User
Tags notification center

First, Notification Center (Nsnotificationcenter)

Each application has a notification hubs (Nsnotificationcenter) instance that is dedicated to assisting different objects

Communication between the messages

• Any object can publish a notification (nsnotification) to the notification center describing what it is doing. Other objects of interest (Observer) can request to be published at a particular notification (or when a specific object is published)

Ii. Notice (nsnotification)

a complete notification typically consists of 3 attributes:-(NSString *) name;//name of the notification- (ID)Object;//notifies the publisher (who wants to post a notification)-(Nsdictionary *) UserInfo;//Some additional information (informing the publisher of the content of the message sent to the notifying recipient)• Initialize a notification (nsnotification) object+ (Instancetype) Notificationwithname: (NSString *) aNameObject:(ID) AnObject; + (Instancetype) Notificationwithname: (NSString *) aNameObject:(ID) AnObject UserInfo: (Nsdictionary *) Auserinfo; -(Instancetype) Initwithname: (NSString *) nameObject:(ID)ObjectUserInfo: (Nsdictionary *) userinfo; A complete notification typically contains 3 attributes:-(NSString *) name;//name of the notification- (ID)Object;//notifies the publisher (who wants to post a notification)-(Nsdictionary *) UserInfo;//Some additional information (informing the publisher of the content of the message sent to the notifying recipient)• Initialize a notification (nsnotification) object+ (Instancetype) Notificationwithname: (NSString *) aNameObject:(ID) AnObject; + (Instancetype) Notificationwithname: (NSString *) aNameObject:(ID) AnObject UserInfo: (Nsdictionary *) Auserinfo; -(Instancetype) Initwithname: (NSString *) nameObject:(ID)ObjectUserInfo: (Nsdictionary *) UserInfo; third, the Announcement Notification Center (Nsnotificationcenter) provides the appropriate method to help publish the notice ·- (void) Postnotification: (nsnotification*) notification; Ø Publish a notification notification, you can set the name of the notification in the notification object, notify the Publisher, additional information, etc. ·- (void) Postnotificationname: (NSString *) Anameobject: (ID) AnObject; Ø Publish a notice called Aname, anobject the publisher of this notice ·- (void) Postnotificationname: (NSString *) aNameObject:(ID) Anobjectuserinfo: (Nsdictionary *) Auserinfo; Ø issued a notice called Aname, AnObject as the publisher of this notice, Auserinfo for additional information four, the registration Notification Listener Notification Center (Nsnotificationcenter) provides a way to register a listener notification of the Listener ( Observer) ·- (void) Addobserver: (ID) Observerselector: (SEL) aselector name: (nsstring*) ANameObject:(ID) AnObject; Øobserver: Listener, that is, who wants to receive this notification Øaselector: After receiving the notification, callback listener this method, and the notification object as a parameter passed in Øaname: the name of the notification. If nil, the listener can receive this notification, regardless of the name of the notification Øanobject: notifies the publisher. If both AnObject and Aname are nil, all notifications are received by the Listener ·- (ID) Addobserverforname: (nsstring*) nameObject:(ID) obj queue: (nsoperationqueue *) Queue usingblock: (void(^) (nsnotification*note)) Block Name: Names of the notifications obj: notifies the publisher Øblock: when a corresponding notification is received, the block queue is called back: determines which operation queue The block executes in, and if nil is passed, the default is to synchronize execution in the current Operation Queue v. Unregister notification Listener · Notification hubs does not hold (retain) listener objects, and objects registered in the notification hub must be unregistered before the object is released. Otherwise, the notification hub will still send a message to the listener when the corresponding notification appears again. Because the corresponding listener object has been freed, it may cause the app to crash • Notification hubs provides a way to unregister listeners o- (void) Removeobserver: (ID) Observer; - (void) Removeobserver: (ID) Observer Name: (NSString *) aNameObject:(IDanobject; generally cancels the registration before the listener destroys it (such as adding the following code to the listener):- (void) Dealloc {//[ Super Dealloc]; this sentence needs to be called in non-arc [[Nsnotificationcenterdefaultcenter] removeobserver:self];The Uidevice informs the Uidevice class that it provides a single-grain object that represents the device through which information about the device can be obtained, such as battery level (batterylevel), battery status (batterystate), type of device ( model, such as ipod, iphone, etc.), device system (systemversion) • This single-grain object can be obtained by [Uidevice Currentdevice] Uidevice object will publish some notifications without interruption, The following is the name constant for the notification published by the Uidevice object: Uideviceorientationdidchangenotification//Device Rotationuidevicebatterystatedidchangenotification//Battery Status Changeuidevicebatteryleveldidchangenotification//Battery charge Changeuideviceproximitystatedidchangenotification//proximity sensors (e.g., the device is close to the user's face)VII. Keyboard Notification • When the system issues keyboard notifications, additional keyboard-related information (dictionaries) is attached, and the dictionary common key is as follows: When the keyboard state changes, the system will issue some specific notifications øuikeyboardwillshownotification//keyboard is about to show Øuikeyboarddidshownotification//Keyboard display Complete øuikeyboardwillhidenotification//keyboard is about to be hidden øuikeyboarddidhidenotification//keyboard hidden Complete øuikeyboardwillchangeframenotification//the position size of the keyboard is about to change øuikeyboarddidchangeframenotification//keyboard position size changedUikeyboardframebeginuserinfokey//the keyboard just started frameUikeyboardframeenduserinfokey//Keyboard final frame (after execution of the animation)Uikeyboardanimationdurationuserinfokey//time of keyboard animationUikeyboardanimationcurveuserinfokey//Keyboard animation execution Rhythm (speed)viii. choice of notification and agent • Common denominator the use of notifications and proxies can accomplish communication between objects (for example, a object tells D objects what happened, a object passes data to D objects) • Different point proxies: one-to-many relationships (an object can only tell what happened to another 1 objects) notification: Many-to-multiple relationships ( 1 objects can tell what happened to n objects, and 1 objects know what happened to n objects .

iOS Development UI article-Notification Center (Nsnotificationcenter)

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.