iOS Development UI Chapter Notification Center (Nsnotificationcenter)

Source: Internet
Author: User

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 contains 3 attributes:

Ø-(nsstring *) name; // name of the notification

Ø-(ID) object; // notify the publisher (who is going to post the notification)

Ø-(nsdictionary *) UserInfo; // Some additional information (informing the publisher of the content of the information delivered to the notifying recipient)

• Initialize a notification (nsnotification) object

Ø+ (instancetype) Notificationwithname: (nsstring *) AName object: (ID) AnObject;

Ø+ (instancetype) Notificationwithname: (nsstring *) AName object: (ID) anobject UserInfo: (nsdictionary *) auserinfo;

Ø-(instancetype) Initwithname: (nsstring *) Name object: (ID) Object userInfo :(nsdictionary *) userInfo;

• A complete notification typically contains 3 attributes:

Ø-(nsstring *) name; // name of the notification

Ø-(ID) object; // notify the publisher (who is going to post the notification)

Ø-(nsdictionary *) UserInfo; // Some additional information (informing the publisher of the content of the information delivered to the notifying recipient)

• Initialize a notification (nsnotification) object

Ø+ (instancetype) Notificationwithname: (nsstring *) AName object: (ID) anobject;

Ø+ (instancetype) Notificationwithname: (nsstring *) AName object: (ID) AnObject userInfo: (nsdictionary *) auserinfo;

Ø-(instancetype) Initwithname: (nsstring *) Name object: (ID) object userInfo: ( Nsdictionary *) UserInfo;

III. Notice of publication

Notification hubs (Nsnotificationcenter) provides the appropriate method to help publish notification-(void) Postnotification: (nsnotification *) notification;

Ø Publish a notification notification to set the name of the notification, the notification publisher , the additional information in the notification object wait

-(void) Postnotificationname: (NSString *) AName object: (ID) anobject; Ø Publish a notification called Aname, anobject the publisher of this notification

-(void) Postnotificationname: (NSString *) AName object: (ID) anobject userInfo: (nsdictionary *) Auserinfo;

Ø Publish a notice called Aname, AnObject as the publisher of this notice, Auserinfo for additional information

IV. Registration Notification Listener

Notification hubs (Nsnotificationcenter) provides a way to register a listener for a listening notification (Observer)

-(void) Addobserver: (ID) Observer selector: (SEL) aselector name: (NSString *) aName

Object: (ID) anobject;

 øobserver: Listener , that is, who will receive this notice øaselector after receiving the notification, this method of callback listener, ø: Name of the notification. If the nil       øanobject: Notifies the publisher. If the anobjectanamenil, listeners receive all notifications

-(ID) Addobserverforname: (NSString *) Name object: (ID) obj queue: (nsoperationqueue *) queue usingblock: (void (^) ( Nsnotification *note)) block;

Øname: Names of notifications

Øobj: Notifies the publisher oblock: When a corresponding notification is received, the block will be recalled

Øqueue: Determines which operation queue The block executes in, and if nilis passed, the default is performed synchronously in the current Operation queue .

V. Cancellation of registration Notification listener

• Notification hubs will not hold (retain) listener objects, 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. The application may crash because the listener object has been freed

• Notification hubs provides the appropriate method to unregister listener ø-(void) Removeobserver: (ID) observer;

Ø-(void) Removeobserver: (ID) Observer name: (nsstring *) AName object: (ID) AnObject;

• Generally unregister before the listener is destroyed (such as by adding the following code to the listener):-(void) Dealloc {

[Super Dealloc]; This sentence needs to be called in non-arc [[Nsnotificationcenter Defaultcenter] removeobserver:self];

}

Vi. Notice of Uidevice

The Uidevice class 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 systems (systemversion)

• This single-grain object can be obtained by [Uidevice Currentdevice]

The Uidevice Object publishes a number of notifications without interruption, and the following is the name constant of the notification published by the Uidevice object:

Ø uideviceorientationdidchangenotification // equipment rotation

Ø uidevicebatterystatedidchangenotification // battery status change

Ø uidevicebatteryleveldidchangenotification // battery charge change

Ø uideviceproximitystatedidchangenotification // proximity sensor (e.g., the device is close to the user's face )

Seven, keyboard notification

• When the system issues keyboard notifications, additional keyboard-related information (dictionaries) are 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 ouikeyboarddidshownotification // Keyboard display finished ouikeyboardwillhidenotification // keyboard is about to hide ØUikeyboarddidhidenotification / / Keyboard hidden complete Øuikeyboardwillchangeframenotification // keyboard position size is about to change o Uikeyboarddidchangeframenotification // keyboard Position size change complete

O

Uikeyboardframebeginuserinfokey//keyboard just started frame

Øuikeyboardframeenduserinfokey//keyboard final frame (after the animation has finished executing)

Øuikeyboardanimationdurationuserinfokey//Keyboard animation time

Øuikeyboardanimationcurveuserinfokey//Keyboard animation execution Rhythm (speed)

Viii. Choice of notice and agent

• Common Ground

Ø Communication between objects can be accomplished using notifications and proxies (e.g. a object tells D objects what's going on, a object passes data to D objects)

• different points

Ø proxy : A pair of relationships (1 objects can only tell the other 1 objects What's going on )

Ø Notification : Many-to-many relationships (1 objects can tell what happens to n objects , and 1 objects know n what happened to an object )

iOS Development UI Chapter 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.