OBJECTIVE-C Study notes: The principle and use of message mechanism

Source: Internet
Author: User
Tags notification center

1. Notification Center (Nsnotificationcenter)

1.1. Each application has a notification hubs (nsnotificationcenter*) instance that is dedicated to assisting with message communication between different objects

1.2. This is the Observer pattern (Observer), and any object can publish a notification (nsnotification*) to the notification center describing what it is doing. Other objects of interest (Observer observer) can request to receive this notification when a particular notification is published (or when a specific object is posted)

2. Notice (nsnotification)

2.1. A complete notification typically contains 3 attributes:

-(nsstring//  notification name -(ID)object//  notification publisher (who wants to post the notification)-( nsdictionary // Some additional information (informing the publisher of the content of the message sent to the notifying recipient)

2.2. Initialize a notification (nsnotification) object

+ (instancetype) Notificationwithname: (nsstringObject:(ID) anobject; + (instancetype) Notificationwithname: (nsstringObject:(ID) anobject userInfo: ( nsdictionary *) auserinfo; -(instancetype) Initwithname: (nsstringObject:(ID)object UserInfo: (nsdictionary *) UserInfo;

3. Release Notifications

3.1. Notification hubs (Nsnotificationcenter) provides the appropriate method to help publish notifications

- (void) Postnotification: (nsnotification*) notification;//Publish a notification notification that sets the name of the notification, the notification Publisher, additional information, and so on in the notification object- (void) Postnotificationname: (NSString*) ANameObject:(ID) AnObject;//publish a notification called Aname, AnObject as the publisher of this notification- (void) Postnotificationname: (NSString*) ANameObject:(ID) AnObject UserInfo: (nsdictionary*) Auserinfo;//publish a notification called Aname, AnObject as the publisher of this notification, auserinfo for additional information

4. Register notification listeners

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

4.1.1

-(void) Addobserver: (ID) Observer selector: (SEL) aselector name: (nsstring  Object:(ID) anobject;

Observer: Listener, who wants to receive this notification

Aselector: After receiving the notification, this method of callback listener, and the notification object as a parameter passed

aName: 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, the listener receives all notifications

4.1.2

-(ID) addobserverforname: (nsstringObject:(ID) obj queue: (nsoperationqueue *) Queue usingblock: (void (^) (nsnotification *note)) block;

Name: Names of notifications

obj: Notifies the publisher

block: When a corresponding notification is received, the block is called back

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

5. Unregister Notification Listener

5.1. Notification hubs does 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

5.2 Notification Hubs provides the appropriate method to unregister the listener

-(void) Removeobserver: (ID) observer; -(void) Removeobserver: (ID) Observer name: (nsstringObject:(ID) AnObject;

5.3 Unregister normally before the listener is destroyed (such as by adding the following code to the listener):

-(void) dealloc {    [[nsnotificationcenter Defaultcenter] Removeobserver: Self];}

6. Notification and selection of agents

6.1. Common Ground

Communication between objects can be accomplished with notifications and proxies

(e.g. a object tells the D object what's going on, a object passes data to the D object)

6.2. Different points

Agent: One-to-two relationships (an object can only tell what happened to another 1 objects)

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

7. Usage of some notification objects

7.1. Uidevice Notice

The 7.1.1. 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)

7.1.2. This singleton object can be obtained by [Uidevice Currentdevice]

7.1.3. Uidevice objects will publish a number of notifications without interruption, and the following are the name constants of the notifications published by the Uidevice object:

Uideviceorientationdidchangenotification//Device rotation

Uidevicebatterystatedidchangenotification//Battery status change

Uidevicebatteryleveldidchangenotification//battery charge change

Uideviceproximitystatedidchangenotification//proximity sensors (e.g., the device is close to the user's face)

7.2. Keyboard notifications

7.2.1. We often need to do certain things when the keyboard pops up or hidden, so we need to listen to the state of the keyboard

7.2.2. When the keyboard state changes, some specific notifications will be sent to the system.

Uikeyboardwillshownotification//keyboard is about to be displayed

Uikeyboarddidshownotification//Keyboard display complete

Uikeyboardwillhidenotification//keyboard is about to be hidden

Uikeyboarddidhidenotification//Keyboard hidden complete

Uikeyboardwillchangeframenotification//Keyboard position size is about to change

Uikeyboarddidchangeframenotification//Keyboard Position size change complete

7.2.3. When the system issues a keyboard notification, a keyboard-related additional information (dictionary) is attached, and the dictionary common key is as follows:

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)

OBJECTIVE-C Study notes: The principle and use of message mechanism

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.