Simple use of iOS NSNotificationCenter
Register the notification in two parts of the project. One is AppDelegate, which simulates the processing of notifications received in the background, and the other is ViewController, which simulates the processing of notifications received by the UI. set a button event in SecondViewController to simulate notification triggering.
Interface preparation:
AppDelegate. m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; ViewController *vc = [[ViewController alloc] init]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc]; self.window.rootViewController = nav; [self.window makeKeyAndVisible]; return YES;}
ViewController has a button (next page) and a label (display notification content)
UIButton * btn = [UIButton buttonWithType :( handle)]; [btn setTitle: @ go to the next page forState :( UIControlStateNormal)]; [btn setTitleColor: [UIColor blackColor] forState :( UIControlStateNormal btn. frame = CGRectMake (100,100,200, 30); [btn addTarget: self action: @ selector (btnAction :) forControlEvents :( UIControlEventTouchUpInside)]; [self. view addSubview: btn];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 200, 30)]; label.tag = 9090; label.textColor = [UIColor blackColor]; label.text = @------; label.textAlignment = NSTextAlignmentCenter; [self.view addSubview:label];
There is a Button in SecondViewController (send a notification triggered by a Button)
UIButton * btn = [UIButton buttonWithType :( handle)]; [btn setTitle: @ triggers the notification forState :( UIControlStateNormal)]; [btn setTitleColor: [UIColor blackColor] forState :( UIControlStateNormal)] btn. frame = CGRectMake (100,100,150, 30); [btn addTarget: self action: @ selector (btnAction :) forControlEvents :( UIControlEventTouchUpInside)]; [self. view addSubview: btn];
// Trigger a notification with a button event-(void) btnAction :( UIButton *) btn {[[nsicationicationcenter defacenter center] postNotificationName: @ CHANGE_LABELTEXT object: @ second page notification content]; [[nsicationcenter center defacenter center] postNotificationName: @ APPDELEGATE_NOTIFICATION object: @ notifies Applegate of the execution event];}
Register two notification centers:
AppDelegate. m
// Register the notification self. After receiving the notification named @ APPDELEGATE_NOTIFICATION, run the notification: Method // nil to indicate that the notification sender is not limited [[nsicationicationcenter defacenter center] addObserver: self selector: @ selector (notification :) name: @ APPDELEGATE_NOTIFICATION object: nil];
// Method for executing after receiving the corresponding notification-(void) notification :( NSNotification *) notification {// notification. name/notification of the name notification. the content of the object notification. You can customize NSLog (% @, notification. object );}
ViewController. m
// Register the notification self. After receiving the notification named @ CHANGE_LABELTEXT, run the notification: Method // nil to indicate that the notification sender is not limited [[nsicationicationcenter defaultCenter] addObserver: self selector: @ selector (notification :) name: @ CHANGE_LABELTEXT object: nil];
// Method for executing after receiving the corresponding notification-(void) notification :( NSNotification *) notification {// notification. name/notification of the name notification. object notification content. You can customize NSString * str = (NSString *) notification. object; (UILabel *) [self. view viewWithTag: 9090]). text = str ;}
When you click the button in SecondViewController, two notifications are sent to notify AppDelegate and ViewController respectively to print and modify the label. Of course, you can also notify the listener in multiple places to perform different operations.