1. Notification: Publish/Listen/remove
- Each application has a notification hubs (Nsnotificationcenter) instance that is specifically designed to assist with message communication between different objects
- Any object can publish a notification (nsnotification) to the notification center, describing what it is doing. Other objects of interest (Observer) can request to receive this notification when a particular notification is published (or when a specific object is posted)
A complete notification typically consists of 3 attributes:
- -(nsstring*) name of the name;//notification
- -(ID) object;//notify the publisher (who is going to post the notice)
- -(nsdictionary*) userinfo;//Some additional information (informing the publisher of the content of the information communicated to the notifying recipient)
Notification (nsnotification) Initializes a notification (nsnotification) object
Publish Notifications
Notification hubs (Nsnotificationcenter) provides the appropriate method to help publish notifications
-(void) Postnotification: (nsnotification*-(voidObject:(ID -(voidObject:(ID) anobject userInfo: (nsdictionary*) auserinfo; publish a notification called Aname, AnObject is the publisher of this notification, auserinfo for additional information
Registering a notification listener
Notification hubs (Nsnotificationcenter) provides a way to register a listener for a listening notification (Observer)
- -(void) Addobserver: (ID) Observer selector: (SEL) aselector name: (nsstring*) AName 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
- -(ID) addobserverforname:(nsstring*) name object:(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.
Unregister Notification Listener
- Notification 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. The application may crash because the listener object has been freed
- 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 [[Nsnotificationcenterdefaultcenter] removeobserver:self];}
Uidevice Notice
- Keyboard notifications
- 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
When the keyboard state changes, some specific notifications will be sent to the system.
When the system issues a keyboard notification, it comes with additional keyboard-related information (a dictionary), and the dictionary common key is as follows:
Selection of 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
- Proxy: 1 objects can only tell another 1 objects what's going on
- Notification: 1 objects can tell what happened to n objects, and 1 objects know what happened to n objects.
click-to-click Method Publish notification, controller supervisor hear notification, call to listen on that methodSequence: Listen first so that the program has started loading the Viewdidload method to listen
Cell:Monitoring notifications
1- (void) Viewdidload {2 [Super Viewdidload];3 //Get the Notification center .4Nsnotificationcenter *center =[Nsnotificationcenter Defaultcenter];5 //Add Listener6[Center addobserver:self selector: @selector (plusclick:) Name:@"plusclicknotification" Object: nil];7 }8 Object: nil]-----Whoever publishes it can9 Observer: ViewerTen //Unregister notification Listener One- (void) Dealloc A { - [[Nsnotificationcenter Defaultcenter] removeobserver:self]; -}
--------------------------------------------------------------------------------------------------------
Controller:#pragma mark-monitoring notification-How to listen for triggering calls
-(void) Plusclick: (nsnotification *) note{ // Remove cell (notification publisher) Xmgwinecell *cell = Note. Object ; // Calculate Total Price int totalprice = self.totalPriceLabel.text.intValue + cell.wine.money.intValue; // Set Total Price Self.totalPriceLabel.text = [NSString stringWithFormat:@ "%d", Totalprice];}
----------------------------------------------------------------------------/**
* Plus click
*/
-(ibaction) Plusclick {//Modifying a modelself.wine.count++; //Modify Quantity LabelSelf.countLabel.text = [NSString stringWithFormat:@"%d", Self.wine.count]; //minus can clickself.minusButton.enabled =YES; //Publish Notifications[[Nsnotificationcenter Defaultcenter] Postnotificationname:@"plusclicknotification" Object: self];}default: Default
Make BY-LJW
Development Notes-Notifications