iOS Pass value-nsnotification

Source: Internet
Author: User

Note: Notification is a very simple and convenient way to send notifications to the page, and the process is that the Publisher publishes the notification to the Notification center, and the subscriber gets the latest notification from the release center, as specified in the structure

Implementation code:

1, create a new single View application project, create a new secondviewcontroller, used as the sending side

2, add UIButton in viewcontroller.m to jump to Secondviewcontroller,uilabel to display notifications

@interfaceViewcontroller () {UILabel*_label;}@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //add UIButton to jump to SecondviewcontrollerUIButton *btn = [[UIButton alloc] Initwithframe:cgrectmake ( -, $, -, -)]; [BTN Settitle:@"Tosecondviewcontroller"Forstate:uicontrolstatenormal]; Btn.backgroundcolor=[Uicolor Graycolor];    [Btn addtarget:self Action: @selector (Tosecondviewcontroller) forcontrolevents:uicontroleventtouchupinside];        [Self.view ADDSUBVIEW:BTN]; //add Uilabel to show values passed by Secondviewcontroller_label = [[UILabel alloc] Initwithframe:cgrectmake ( -, -, -, -)]; _label.backgroundcolor=[Uicolor Yellowcolor];    [Self.view Addsubview:_label]; }- (void) Tosecondviewcontroller {Secondviewcontroller*second =[[Secondviewcontroller alloc] init]; [Self Presentviewcontroller:second animated:yes completion:nil];}

3, add UIButton in SECONDVIEWCONTROLLER.M to send notifications and jump back to Viewcontroller,uilabel for input notification data

@interfaceSecondviewcontroller () {Uitextfield*_textfield;}@end@implementationSecondviewcontroller- (void) viewdidload {[Super viewdidload]; Self.view.backgroundColor=[Uicolor Whitecolor]; //Add a button to return ViewcontrollerUIButton *btn = [[UIButton alloc] Initwithframe:cgrectmake ( -, $, -, -)]; [BTN Settitle:@"Backtoviewcontroller"Forstate:uicontrolstatenormal]; Btn.backgroundcolor=[Uicolor Bluecolor];    [Btn addtarget:self Action: @selector (Postnotification) forcontrolevents:uicontroleventtouchupinside];        [Self.view ADDSUBVIEW:BTN]; //Add a Uitextfield_textfield = [[Uitextfield alloc] Initwithframe:cgrectmake ( -, -, -, -)]; _textfield.backgroundcolor=[Uicolor Greencolor]; [Self.view Addsubview:_textfield];}

4, implementing the Send notification method in SECONDVIEWCONTROLLER.M

-(void  ) postnotification {nsdictionary 
    *dict = [[Nsdictionary alloc] Initwithobjectsandkeys:_textfield.text,@ " Span style= "color: #800000;"    >test    nil];  //  Create notification  Nsnotification * Notification =[nsnotification notificationwithname:@ " getvalue   " object     :nil userinfo:dict];  //  send notification via notification center   [[Nsnotificationcenter Defaultcenter] postnotification:notification]; [Self Dismissviewcontrolleranimated:yes completion:nil];}  

5, register notification in VIEWCONTROLLER.M, and implement registration notification method

#import "ViewController.h"#import "SecondViewController.h"@interfaceViewcontroller () {UILabel*_label;}@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //add UIButton to jump to SecondviewcontrollerUIButton *btn = [[UIButton alloc] Initwithframe:cgrectmake ( -, $, -, -)]; [BTN Settitle:@"Tosecondviewcontroller"Forstate:uicontrolstatenormal]; Btn.backgroundcolor=[Uicolor Graycolor];    [Btn addtarget:self Action: @selector (Tosecondviewcontroller) forcontrolevents:uicontroleventtouchupinside];        [Self.view ADDSUBVIEW:BTN]; //add Uilabel to show values passed by Secondviewcontroller_label = [[UILabel alloc] Initwithframe:cgrectmake ( -, -, -, -)]; _label.backgroundcolor=[Uicolor Yellowcolor];            [Self.view Addsubview:_label]; //Registration notice [[nsnotificationcenter defaultcenter] addobserver:self selector: @selector (getnotification:) name:@ " GetValue "Object : nil]; }- (void) Tosecondviewcontroller {Secondviewcontroller*second =[[Secondviewcontroller alloc] init]; [Self Presentviewcontroller:second animated:yes completion:nil];}//Implement registration notification method, assign value to label-(void) GetNotification: (nsnotification*) notification{nsdictionary *dict = [notification    UserInfo];    _label.text = [dict objectforkey:@ "test"]; }@end

Source Address: Https://github.com/rokistar/PassValueUsingNotification

iOS Pass value-nsnotification

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.