Notification hubs (Nsnotificationcenter) actually provide a broadcast mechanism within the program. The received message is forwarded to the desired object according to the internal message forwarding. In fact, this sentence has clearly told us how to use the notice. The first step: register the notice to be observed in the place where needed, step two: send a notification somewhere. (Note here: The notifications sent may be either custom or system-defined).
One, use notification
1th. Create Notification method
Note that the use of notifications is sequential
Be sure to listen to the notification first and then send the notification
// first method add a notification [[Nsnotificationcenter Defaultcenter] Addobserver:self Selector: @selector (addnotification) name:@ " object :nil]; // send a notification [[ Nsnotificationcenter Defaultcenter] Postnotificationname:@ " tongzhi object : nil];
// sending notifications can also pass some parameters Object ID) #> userinfo:<# (Nullable nsdictionary *) #>
Information that can be notified in a way that listens for notifications
-(void) addnotification { NSLog (@ " accept notification ");}
Finally, to remove the notification
-(void) dealloc { // remove observerself [[Nsnotificationcenter Defaultcenter] removeobserver:self];}
The use of block 2nd is convenient and simple
This method requires the value of an ID type to be accepted
ID observe;
//The second method of//Name: Names of notifications//object: Who sent the notification//queue: Queues, determines which thread the block executes in, Nil executes in the thread that issued the notification//Usingblock: As long as the supervisor hears the notice, it will execute the block.//This notification returns an ID that also needs to be removed_observe = [[Nsnotificationcenter defaultcenter] Addobserverforname:@"Tongzhi" Object: Nil Queue:nil usingblock:^ (nsnotification *_nonnull Note) {NSLog (@"received a notification"); }]; [[Nsnotificationcenter Defaultcenter] Postnotificationname:@"Tongzhi" Object: nil];
Again, there is a need to remove the notice, but the observer here is not self but _observe
-(void) dealloc { // Remove observer _observe [[Nsnotificationcenter Defaultcenter] removeobserver:_observe];}
Two. Notifications for use in multi-threading
Notifications are used in multiple threads
Notifications are received in the same way that the notification is sent to the same thread
Asynchronously sends a notification, the main thread listens to the notification, and the method that receives the notification is in the child thread
- (void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event { //Send NotificationsDispatch_async (Dispatch_get_global_queue (0,0), ^{[[Nsnotificationcenter defaultcenter] Postnotificationname:@"Tongzhi" Object: nil]; });}- (void) viewdidload {[Super viewdidload]; //use notifications in multiple threads[[Nsnotificationcenter defaultcenter] addobserver:self selector: @selector (addnotification) Name:@"Tongzhi" Object: nil]; }- (void) addnotification {NSLog (@"Accept Notifications"); NSLog (@"%@", [Nsthread CurrentThread]); //Main thread: Listen for notifications, send notifications asynchronously//Summary: The code that receives the notification is sent by the thread that issued the notification//Update UIDispatch_sync (Dispatch_get_main_queue (), ^{ //Update UI });}
Console output: Asynchronous
£ º05.537 notificationmultithreading use [5798:299202 ] (notice) ---: £ 05.538 Notify multithreading using [5798:2992020x60800026d8c04, name = (null)}
Send notifications in the main thread, listen for notifications in asynchronous threads
- (void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event { //sending notifications in the main thread[[Nsnotificationcenter Defaultcenter] Postnotificationname:@"Tongzhi" Object: nil];}- (void) viewdidload {[Super viewdidload]; //use notifications in multiple threads//Asynchronous Listener NotificationsDispatch_async (Dispatch_get_global_queue (0,0), ^{[[Nsnotificationcenter defaultcenter] addobserver:self selector: @selector (addnotification) Name:@"Tongzhi" Object: nil]; });}- (void) addnotification {NSLog (@"Accept Notifications"); NSLog (@"%@", [Nsthread CurrentThread]); //Asynchronous: Listen for notifications, the main thread to send notifications//Summary: The code that receives the notification is sent by the thread that issued the notification}
Console output
£ º40.160 notificationmultithreading use [5879:321079 ] received notice : £40.160 Notify multithreading using [5879:3210790x600000072ac01, name = main}
Use block to create a notification method,
Queue: [Nsoperationqueue Mainqueue] This will execute block code in the main thread
_observe = [[Nsnotificationcenter defaultcenter] Addobserverforname:@ "tongzhi" Object: Nil queue:[nsoperationqueue Mainqueue] usingblock:^ (nsnotification * _nonnull Note) { NSLog ( @" received a notification " ); }];
Console output: Executing in the main thread
£ º39.277 notificationmultithreading use [5979:332663 ] received notice : £ º39.277 Notify multithreading using [5979:3326630x60000006ea401, name = main}
Notification Center Nsnotificationcenter App Summary in iOS