IOS notification mechanism

Source: Internet
Author: User

1 Announcement of notifications

**通知中心(NSNotificationCenter)**每一个应用程序都有一个通知中心实例,专门来负责协助不同对象之间的消息通信任何一个对象都可以向通知中心发布通知,描述自己在做什么,其他感兴趣的对象可以申请在某个特定通知发布时(或者在某个特定的对象发布通知时)收到通知![如下所示](http://img.blog.csdn.net/20160424124752621)**通知(NSNotification)**一个完整的通知一般包含3个属性:
- (NSString *)name;   //通知名称    - (id)object;  // 通知发布者 (是谁要发布通知)    - (NSDictionary *)userInfo; //一些额外的信息(通知发布者传递给通知接收者的信息内容)
初始化一个通知对象
+ (instancetype) notificationWithName: (NSString *)aName object: (id)onObject;+  (instancetype) notificationWithName:(NSString *)aName object:(id)onObject userInfo:(NSDictionary *aUserInfo);- (instancetype)initWithName:(NSString *)aName object:(id) object userInfo: (NSDictionary *)userInfo;
**发布通知**通知中心提供了相应的方法来发布通知
- (void) postNotification:(NSNotification//发布一个notification通知,可在notification最想中设置通知的名称、通知发布者,额外信息等    - (void) postNotificationName:(NSString *)aName object:(id)anObject;//发布一个名称为aName的通知,anObject为这个通知的发布者    - (void)postNotificationName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary  //发布一个名称为aName的通知,anObject为这个通知的发布者,aUserInfo为额外信息

2 Notification of monitoring

**注册通知监听器**通知中心提供了方法来注册一个监听通知的监听器(observer)- (void) addObserver:(id) observer selector: (SEL) aSeletor name: (NSString *)aName object: (id)anObject;//observer:监听器,谁要接听这个通知//aSelector: 收到通知后,回调监听器的这个方法,并且把通知对象当做参数传入// aName:通知的名称,如果为nil ,那么无论通知的名称是什么,监听器都能收个这个通知//anObject:通知发布者,如果为anObject和aName都为nil,监听器都受到所有的通知- (id) addObserverForName:(NSString *)name object: (id)obj queue: (NSOperationQueue *)queue usingBlock: (void (^)(NSNotification *note))block;//name : 通知的名称//obj: 通知发布者//block:收到对应的通知时,会回调哪个block//queue: 决定了block在哪个操作队列中执行,如果传nil,默认在当前队列中同步执行。

3. Removal of notifications

Notification hubs does not retain listener objects, objects registered in notification hubs must be unregistered before the object is released, otherwise notification hubs will still send messages to the listener when the corresponding notification reappears because the listener object has been freed, which could cause the app to crash
Unregister listener:
Notification hubs does not retain listener objects, objects registered in notification hubs must be unregistered before the object is released, otherwise notification hubs will still send notifications to the listener when the corresponding notification reappears, but the corresponding listener has been released, which could cause the app to crash

-(void) Removeobserver: (ID) observer;
-(void) Removeobserver: (ID) Observer name: (NSString *) AName (ID) object; Unregister before the listener is destroyed

- (void) dealloc {    [[NSNotificationCenterself];}

Give me a chestnut.

Give an example of a magazine company sending a magazine

I create a new two class, one is the Czperson class, the other is the Czcompany class, each defines a Name property

The person object subscribes to company's notifications, and company publishes a notification,

The Receivemagazine method is called after the person receives the notification:

To cancel the listener:

Run:

Examples of many notifications used in the system

    1. Uidevice Notice:
      The Uidevice class provides a singleton 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.). ), the system of the device (systemversion)

This singleton 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//Device rotation
Uidevicebatterystatedidchangenotification//Battery status change
Uidevicebatteryleveldidchangenotification//battery charge change
Uideviceproximitystatedidchangenotification//proximity sensors (e.g., the device is close to the user's face)

    1. Keyboard Notifications:

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

When the system issues a keyboard notification, it comes with additional keyboard-related information (a dictionary), 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)

The same points and different points for notifications and proxies:

Common
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)

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)

IOS notification mechanism

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.