#import"ViewController.h"#import"SViewController.h"@interface Viewcontroller () @property (weak, nonatomic) Iboutlet UILabel*ShowLabel, @end @implementation viewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib. //1. Register as an observer and listen for notifications in B view[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (amethod:) Name:@"Mynotificationname" Object: nil];}-(void) Amethod: (Nsnotification *) notification{//2. Get the data that the notification carries, update the text information for the labelNsdictionary *dictdata =[Notification UserInfo]; NSString*STR = [Dictdata objectforkey:@"Myuserinfokey"]; Self.showLabel.text=str;}-(ibaction) GOSVC: (ID) sender {sviewcontroller*svc =[[Sviewcontroller alloc] init]; [Self presentviewcontroller:svc animated:yes completion:^{ } ];}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //3. Remove all Notifications[[Nsnotificationcenter defaultcenter]removeobserver:self];} @end-----------#import"SViewController.h"@interface Sviewcontroller () @property (Strong, nonatomic) Uitextfield*TextField, @end @implementation sviewcontroller- (void) viewdidload {[Super viewdidload]; //Do any additional setup after loading the view.Self.view.backgroundColor =[Uicolor Redcolor]; Self.textfield= [[Uitextfield alloc] Initwithframe:cgrectmake ( -, $, -, -)]; Self.textField.backgroundColor=[Uicolor Whitecolor]; [Self.view AddSubview:self.textField]; UIButton*BTN =[UIButton Buttonwithtype:uibuttontyperoundedrect]; Btn.frame= CGRectMake ( -, -, -, -); [BTN Settitle:@"Go"Forstate:uicontrolstatenormal]; [Self.view ADDSUBVIEW:BTN]; [Btn addtarget:self Action: @selector (CLICKGOVC:) forcontrolevents:uicontroleventtouchupinside]; }- (void) CLICKGOVC: (ID) Sender {//return to view a and publish notifications[Self Dismissviewcontrolleranimated:yes completion:^{ //1. Create the information that UserInfo carriesNSString *str =Self.textField.text; Nsdictionary*dictdata = [Nsdictionary dictionarywithobject:str forkey:@"Myuserinfokey"]; //2. Release information[[Nsnotificationcenter Defaultcenter] Postnotificationname:@"Mynotificationname" Object: nil Userinfo:dictdata]; }];}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.} @end
IOS-Notifications (nsnotification)