標籤:
說明:通知在頁面傳值中,通知是非常簡單便捷的方式,具體過程是,發行者發布通知到通知中樞,訂閱者從發布中心獲得最新通知,具體結構如
實現代碼:
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