Simple use of iOS NSNotificationCenter

Source: Internet
Author: User

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.

 

Related Article

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.