Ios notification mechanism

Source: Internet
Author: User
Tags notification center

Ios notification mechanisms, including notification Publishing, notification listening, and notification removal.

1. Notification Publishing

Before publishing a notification, you must create a notification object. A notification object includes

 

 

Notification Publisher: send a notification to the notification center;

Notification center: receives notifications from the notification publisher and forwards them to the notification recipient.

Notification recipient: receives notifications from the notification center.

Content to be included in a notification object:

1. (NSString *) name; // notification name

2. (id) object; // notify the publisher

3. (NSDictionary *) userInfo; // some additional information (the information that the notification publisher transmits to the notification recipient)

II,

1. Create a notification (NSNotification) object method:

+ (Instancetype) notificationWithName :( NSString *) name object :( id) Object;

+ (Instancetype) notificationWithName :( NSString *) name object :( id) Object userInfo :( NSDictionary *) UserInfo;

-(Instancetype) initWithName :( NSString *) name object :( id) object userInfo :( NSDictionary *) userInfo;

 

 

2. The nsicationicationcenter provides methods to help publish notifications.

-(Void) postNotification :( NSNotification *) notification;

Publish a notification. You can set the notification name, publisher, and additional information in the notification object.

 

-(Void) postNotificationName :( NSString *) Name object :( id) Object;

Publish a notification named Name. The Object is the publisher of the notification.

 

-(Void) postNotificationName :( NSString *) Name object :( id) Object userInfo :( NSDictionary *) UserInfo;

Publish a notification named name. The object is the notification publisher, and userinfo is the additional information.

 

 

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

-(Void) addObserver :( id) observer selector :( SEL) Selector name :( NSString *) Name object :( id) Object;

Observer: Listener, that is, who wants to receive this notification

Selector: After receiving the notification, callback the method of the listener and pass the notification object as a parameter.

Name: the Name of the notification. If it is nil, the listener will receive the notification no matter what the notification name is.

Object: Notification publisher. If both anObject and aName are nil, the listener receives all notifications.

 

-(Id) addObserverForName :( NSString *) name object :( id) obj queue :( NSOperationQueue *) queue usingBlock :( void (^) (NSNotification * note) block;

Name: Notification name

Obj: Notification publisher

Block: When a notification is received, the block is called back.

Queue: determines the operation queue in which the block is executed. If nil is passed, it is executed synchronously in the current operation queue by default.

 

4. The notification center will not retain the listener object. The object registered in the notification center must be canceled before the object is 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 methods to cancel listener registration.

-(Void) removeObserver :( id) observer;

-(Void) removeObserver :( id) observer name :( NSString *) Name object :( id) Object;

 

Generally, cancel registration before the listener is destroyed (for example, add the following code to the listener ):

-(Void) dealloc {

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

[[Nsnotificationcenterdefacenter] removeObserver: self];

For example:

We often need to do 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 will be displayed soon

UIKeyboardDidShowNotification // the keyboard is displayed

UIKeyboardWillHideNotification // the keyboard is about to be hidden

UIKeyboardDidHideNotification // the keyboard is hidden.

UIKeyboardWillChangeFrameNotification // The position and size of the keyboard are about to change.

UIKeyboardDidChangeFrameNotification // The keyboard position and size have been changed

When the system sends a keyboard notification, additional information (dictionary) related to the keyboard is attached. The common keys in the dictionary are as follows:

UIKeyboardFrameBeginUserInfoKey // frame at the beginning of the keyboard

UIKeyboardFrameEndUserInfoKey // The final frame of the keyboard (after the animation is executed)

UIKeyboardAnimationDurationUserInfoKey // time of keyboard Animation

UIKeyboardAnimationCurveUserInfoKey // execution speed of the keyboard animation (fast and fast)

Because the system automatically generates a notification on the keyboard, you do not need to create a notification. Instead, you only need to register a listener. When you receive the notification, execute the method.

 

 

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.