11. Notification Mechanism for ios, 11. ios Mechanism

Source: Internet
Author: User

11. Notification Mechanism for ios, 11. ios Mechanism


Notification center (nsicationicationcenter)

• Each application has an nsicationicationcenter instance to assist in message communication between different objects. • any object can send a notification (NSNotification) to the notification center ), describe what you are doing. Other Objects of interest (Observer) can apply to receive the notification when a specific notification is published (or when a specific object is published ).



Notification (NSNotification)

• A complete notification generally contains three attributes: Ø-(NSString *) name; // notification name Ø-(id) object; // notification publisher (who wants to publish the notification) Ø-(NSDictionary *) userInfo; // some additional information (the information that the notification publisher sends to the notification recipient) • initialize a notification (NSNotification) object Ø + (instancetype) notifwitwithname :( NSString *) aName object :( id) anObject; Ø + (instancetype) notificationWithName :( NSString *) aName object :( id) anObjectuserInfo :( NSDictionary *) aUserInfo; Ø-(instancetype) initWithName :( NSString *) name object :( id) objectuserInfo :( NSDictionary *) userInfo;

Release notification

The notification center (nsicationicationcenter) provides methods to help publish notifications.

•-(Void) postNotification :( NSNotification *) notification; Ø publish a notification and set the notification name, notification publisher, and additional information in the notification object. •-(void) posticationicationname :( NSString *) aName object :( id) anObject; Ø publish a notification named aName, and anObject is the publisher of the notification •-(void) postNotificationName :( NSString *) aName object :( id) anObjectuserInfo :( NSDictionary *) aUserInfo;

Register notification listener

The notification center (nsicationicationcenter) provides a method to register an listener (Observer) that listens to notifications)

•-(Void) addObserver :( id) observerselector :( SEL) aSelectorname :( NSString *) aName object :( id) anObject; Ø observer: Listener, that is, who wants to receive this notification Ø aSelector: after receiving the notification, callback the method of the listener and pass the notification object as a parameter. If it is nil, the listener will receive this notification regardless of the notification name. Ø anObject: the notification publisher. If both anObject and aName are nil, the listener receives all notifications.


•-(Id) addObserverForName :( NSString *) name object :( id) objqueue :( NSOperationQueue *) queue usingBlock :( void (^) (NSNotification * note) block; name Ø: notification name Ø obj: Notification publisher Ø block: When a corresponding notification is received, this block is called back. Ø queue: determines the action queue in which the block is executed. If nil is passed, synchronous execution in the current operation queue by default

Cancel registration notification listener

• The notification center does not retain listener objects. The objects registered in the notification center must be canceled before the objects are released. Otherwise, when the corresponding notification appears again, the notification center will still send a message to the listener. Because the listener object has been released, the application may crash. • The notification center provides a method to cancel the registration of the listener. (void) removeObserver :( id) observer; (void) removeObserver :( id) observername :( NSString *) aName object :( id) anObject; • Generally, the registration is canceled before the listener is destroyed (such as adding the following code to the listener ):

-(Void) dealloc {

// [Superdealloc]; this sentence needs to be called in non-ARC

[[Nsnotificationcenterdefacenter] removeObserver: self];

}


UIDevice notification

• The UIDevice class provides a single object, which represents a device and can be used to obtain device-related information, such as batteryLevel and batteryState), device type (model, such as iPod, iPhone, etc.), device system (systemVersion) • [UIDevicecurrentDevice] can be used to obtain this single object • The UIDevice object will continuously publish some notifications. The following are the name constants of notifications published by the UIDevice object: Ø UIDeviceOrientationDidChangeNotification // The device rotates. Ø UIDeviceBatteryStateDidChangeNotification // The Battery status changes. Ø change battery power. Ø // close sensor (for example, the device is close to the user's face) • keyboard notification

• We often need to perform some specific operations when the keyboard is popped up or hidden, so we need to listen to the keyboard status • when the keyboard status changes, the system will send some specific notifications. Ø UIKeyboardWillShowNotification // the keyboard is about to display. Ø UIKeyboardDidShowNotification // the keyboard is displayed. Ø uikeyboardwillenoenotification // the keyboard is about to be hidden. Summary // the keyboard is hidden. the position and size of the keyboard are about to change. Ø UIKeyboardDidChangeFrameNotification // The position and size of the keyboard have been changed.

• When the system sends a keyboard notification, additional keyboard-related information (dictionary) is attached. The common keys in the dictionary are as follows: Ø UIKeyboardFrameBeginUserInfoKey // frame at the beginning of the keyboard Ø UIKeyboardFrameEndUserInfoKey // final frame of the keyboard (after the animation is completed) Ø UIKeyboardAnimationDurationUserInfoKey // time for keyboard animation Ø UIKeyboardAnimationCurveUserInfoKey // speed of keyboard animation execution)
Notification and proxy Selection

• Commonalities-communication between objects can be completed using notifications and Proxies

(For example, object A tells object D what happened and object A passes data to object D)

• Differences Ø Proxy: one-to-one relationship (one object can only tell the other object what happened) Ø notification: many-to-many relationship (one object can tell N objects what happened, And one object can know what happened to N objects)




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.