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