iOS傳值-NSNotification

來源:互聯網
上載者:User

標籤:

說明:通知在頁面傳值中,通知是非常簡單便捷的方式,具體過程是,發行者發布通知到通知中樞,訂閱者從發布中心獲得最新通知,具體結構如

實現代碼:

1,建立Single View Application工程,建立一個SecondViewController,用於作為發送端

2,在ViewController.m中添加UIButton用於跳轉到SecondViewController,UILabel用於顯示通知

@interface ViewController (){    UILabel *_label;}@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    //添加UIButton,用於跳轉到SecondViewController    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(20, 200, 300, 44)];    [btn setTitle:@"ToSecondViewController" forState:UIControlStateNormal];    btn.backgroundColor = [UIColor grayColor];    [btn addTarget:self action:@selector(toSecondViewController) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:btn];        //添加UILabel,用於顯示SecondViewController傳過來的值    _label = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 300, 44)];    _label.backgroundColor = [UIColor yellowColor];    [self.view addSubview:_label];    }- (void)toSecondViewController {    SecondViewController *second = [[SecondViewController alloc] init];    [self presentViewController:second animated:YES completion:nil];}

 

3,在SecondViewController.m中添加UIButton,用於發送通知並且跳回ViewController,UILabel用於輸入通知數據

@interface SecondViewController (){    UITextField *_textField;}@end@implementation SecondViewController- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor = [UIColor whiteColor];    //添加一個按鈕用於返回ViewController    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(20, 200, 300, 44)];    [btn setTitle:@"BackToViewController" forState:UIControlStateNormal];    btn.backgroundColor = [UIColor blueColor];    [btn addTarget:self action:@selector(postNotification) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:btn];        //添加一個UITextField    _textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, 300, 44)];    _textField.backgroundColor = [UIColor greenColor];    [self.view addSubview:_textField];}

4,在SecondViewController.m中實現發送通知方法

- (void)postNotification {    NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:_textField.text,@"test" ,nil];    //建立通知    NSNotification *notification =[NSNotification notificationWithName:@"getValue" object:nil userInfo:dict];    //通過通知中樞發送通知    [[NSNotificationCenter defaultCenter] postNotification:notification];    [self dismissViewControllerAnimated:YES completion:nil];}

5,在ViewController.m中註冊通知,並且實現註冊通知方法

#import "ViewController.h"#import "SecondViewController.h"@interface ViewController (){    UILabel *_label;}@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    //添加UIButton,用於跳轉到SecondViewController    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(20, 200, 300, 44)];    [btn setTitle:@"ToSecondViewController" forState:UIControlStateNormal];    btn.backgroundColor = [UIColor grayColor];    [btn addTarget:self action:@selector(toSecondViewController) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:btn];        //添加UILabel,用於顯示SecondViewController傳過來的值    _label = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 300, 44)];    _label.backgroundColor = [UIColor yellowColor];    [self.view addSubview:_label];            //註冊通知    [[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];}//實現註冊通知方法,給label賦值-(void)getNotification:(NSNotification*)notification{    NSDictionary *dict = [notification userInfo];    _label.text = [dict objectForKey:@"test"];    }@end

源碼地址:https://github.com/rokistar/PassValueUsingNotification

 

iOS傳值-NSNotification

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.