[iOS base Control-6.10] Notification notification mechanism

Source: Internet
Author: User

A. DefinitioniOS programs have a single Nsnotificationcenter object that is responsible for publishing notifications between different objects, any object that can publish notifications in Nsnotificationcenter, and publish their own occurrences. Listener (Observer) can choose to accept a specific notification.   B. Use 1. Properties
    • -(nsstring*) name; Name of the notification
    • -(ID) object; Notifies the publisher (who wants to post a notification)
    • -(nsdictionary*) UserInfo; Some additional information (informing the publisher of the content of the message sent to the notifying recipient)
2. Initialize
    • + (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;
3. Announcement (via Nsnotificationcenter)-(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*) 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 notification called Aname, AnObject as the publisher of this notification, auserinfo for additional information 4. Set Listener ObserverNotification hubs (Nsnotificationcenter) provides a way to register a listener for a listening notification (Observer) (1)-(void) Addobserver: (ID) Observer selector: (SEL)      Aselector Name: (nsstring*) AName object: (ID) anobject; Observer: Listener, that is, who wants to receive this notification Aselector: After receiving the notification, callback listener this method, and the notification object as a parameter passed in AName: the 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 (2)-(ID) Addobserverforname: (nsstring*) Name object: (ID) obj queue: (     nsoperationqueue*) Queue usingblock: (void (^) (nsnotification*note)) block; Name: Names of the notifications obj: Notifies the publisher of Block: When a corresponding notification is received, the block queue is called back: determines which operation queue The block executes in, and if nil is passed, the default is performed synchronously in the current Operation queue 5. Before the listener destroys, the listener is canceledNotification hubs does not hold (retain) listener objects, and 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.       Because the corresponding listener object has been freed, it may cause the app Crash Notification Center to provide the appropriate method to unregister the 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];

}

C.ios Notification UideviceThe 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 device's system (Systemversion) can obtain this singleton object through [Uidevice Currentdevice] Uidevice object will publish some notifications without interruption, the following is the name of the notification published by the Uidevice object Constant: uideviceorientationdidchangenotification//device rotation uidevicebatterystatedidchangenotification//battery status change UIDevi Cebatteryleveldidchangenotification//battery Change uideviceproximitystatedidchangenotification//proximity sensor (e.g. the device is close to the user's face) D. Keyboard Notifications          we often need to do certain things when the keyboard pops up or hidden, so we need to monitor the status of the keyboard             the system will issue some specific notifications when the keyboard status changes      Uikeyboardwillshownotification//keyboard is about to show      uikeyboarddidshownotification//keyboard display complete       uikeyboardwillhidenotification//keyboard is about to be hidden      uikeyboarddidhidenotification/ /keyboard Hidden complete      uikeyboardwillchangeframenotification//keyboard location 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 (dictionary), the dictionary common key is as follows:     Uikeyboardframebeginuserinfokey//keyboard just start frame      Uikeyboardframeenduserinfokey//keyboard final frame (after execution of the animation)      Uikeyboardanimationdurationuserinfokey//Keyboard animation time      Uikeyboardanimationcurveuserinfokey// Keyboard animation execution Rhythm (speed)   E. Similarities and differences of notice and agentCommon ground the use of notifications and proxies can accomplish communication between objects (for example, a object tells D objects what is going on, a object passes data to D objects) different proxies: one to the other (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) F. PracticeAnalog enemy stations to release information, our personnel receive our radio station, enemy personnel to receive enemy radio, the mole can receive both sides of the radio
1 //2 //Radio.h3 //Notification4 //5 //Created by Hellovoidworld on 14/12/7.6 //Copyright (c) 2014 Hellovoidworld. All rights reserved.7 //8 9 #import<Foundation/Foundation.h>Ten  One @interfaceRadio:nsobject A  - //Station name -@property (nonatomic, copy) NSString *name; the  - @end
1 //2 //RADIO.M3 //Notification4 //5 //Created by Hellovoidworld on 14/12/7.6 //Copyright (c) 2014 Hellovoidworld. All rights reserved.7 //8 9 #import "Radio.h"Ten  One @implementationRadio A  - @end
1 //2 //main.m3 //Notification4 //5 //Created by Hellovoidworld on 14/12/7.6 //Copyright (c) 2014 Hellovoidworld. All rights reserved.7 //8 9 #import<Foundation/Foundation.h>Ten #import "Radio.h" One #import "Person.h" A  - intMainintargcConst Char*argv[]) { - @autoreleasepool { theRadio *R1 =[[Radio alloc] init]; -R1.name =@"our Radio"; -         -Radio *R2 =[[Radio alloc] init]; +R2.name =@"Enemy Radio"; -         +         //can only receive our radio messages APerson *P1 =[[Person alloc] init]; atP1.role =@"our loyal Warriors."; -         -         //can only receive enemy radio messages -Person *P2 =[[Person alloc] init]; -P2.role =@"the evil minions of the enemy"; -         in         //ability to receive messages from our radio station and receive enemy radio messages -Person *P3 =[[Person alloc] init]; toP3.role =@"lurking in our cunning mole."; +         -         //Well, I'm the medium for transmitting radio waves. theNsnotificationcenter *center =[Nsnotificationcenter Defaultcenter]; *         $         //1. Adding listenersPanax Notoginseng[Center ADDOBSERVER:P1 selector: @selector (receiveinfofromradio:) Name:@"Attack" Object: R1]; -[Center ADDOBSERVER:P2 selector: @selector (receiveinfofromradio:) Name:@"Defend" Object: R2]; the[Center ADDOBSERVER:P3 selector: @selector (receiveinfofromradio:) Name:@"Attack" Object: R1]; +[Center ADDOBSERVER:P3 selector: @selector (receiveinfofromradio:) Name:@"Defend" Object: R2]; A         the         //2. Our Radio R1 release attack information +[Center Postnotificationname:@"Attack" Object: R1 userinfo:@{@"title":@"attack!, all the money belong to Us!",@"Title2":@"and the girls!!!"}]; -         $         //3. Enemy Radio R2 release Fall back information $[Center Postnotificationname:@"Defend" Object: R2 userinfo:@{@"title":@"TP back quickly!"}]; -         -     } the     return 0; -}
Out 2014-12-07 21:18:03.142 notification[7923:714964] I am a loyal warrior. , we're receiving news from our station. , content is {
title = "attack!, all the money belong to Us!";
Title2 = "and the girls!!!";
}
2014-12-07 21:18:03.143 notification[7923:714964]
I'm a mole lurking in our cunning. , we're receiving news from our station. , content is {
title = "attack!, all the money belong to Us!";
Title2 = "and the girls!!!";
}
2014-12-07 21:18:03.143 notification[7923:714964]
I'm an enemy evil dog. , receiving messages from enemy stations , content is {
title = "TP back quickly!";
}
2014-12-07 21:18:03.143 notification[7923:714964]
I'm a mole lurking in our cunning. , receiving messages from enemy stations , content is {
title = "TP back quickly!";
}
Program ended with exit code:0

[iOS base Control-6.10] Notification 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.