IOS-Notifications (nsnotification)

Source: Internet
Author: User

#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)

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.