通知传值与协议传值都是反向的传值方式,即从第二个界面传到第一个界面。
首先确定传值界面需要做的:
- 传值的发起者是第二层界面,第二层界面作为传值的发起者,需要明确它是要注册通知
注册通知:
复制代码
1
2
3
4
5//ChangeValue即为通知名称,需要与注册监听者中的通知名称保持一致 //object:需要与注册监听者中的object保持一致,除非注册监听者中的object为nil。 //userInfo:信息字典,包含了有关通知的可选信息 [[NSNotificationCenter defaultCenter]postNotificationName:@"ChangeValue" object:nil userInfo:@{@"Value":_textF.text}];
然后确定接收值界面需要做的:
- 传值的接收者是第一层界面,第一层界面作为传值的接收者,需要明确它是要注册监听者,同时在监听到通知时执行一个我们需要的方法,并且在通知发送完之后进行移除
注册监听者:
复制代码
1
2
3
4
5
6
7
8//addObserver:监听通知的对象 //selector:监听到通知时所执行的方法 //name:通知名称,与注册通知的name保持一致 //object:由于通知的查找方式时先找监听者,再根据监听者找通知,所以object写成nil就代表可以接收使用通知发来的数据, //但是如果写成self,就会从注册中心找self,如果注册通知写的不是self,就会找不到,所以需要注册通知的object向注册监听者的object适应 //object一般可以都写成nil,不易出错 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(ChangeTextFText:) name:@"ChangeValue" object:nil];
需要的方法:
利用这个自定义方法来完成想要传的各种值
移除:
复制代码
1
2
3//写在接收值界面也是第一界面,在完成传值后进行移除通知,防止内存泄露 [[NSNotificationCenter defaultCenter]removeObserver:self];
第一界面:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44#import "ViewController.h" #import "SecondViewController.h" @interface ViewController () @property (nonatomic,strong) UITextField *textField; @property (nonatomic,strong) UIButton *nextBtn; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.navigationItem.title = @"通知传值"; self.view.backgroundColor = [UIColor yellowColor]; //创建textField _textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 50)]; _textField.borderStyle = UITextBorderStyleRoundedRect; _textField.backgroundColor = [UIColor whiteColor]; //创建button _nextBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; _nextBtn.frame = CGRectMake(20, 200, self.view.bounds.size.width - 40, 44); _nextBtn.backgroundColor = [UIColor blueColor]; _nextBtn.titleLabel.font = [UIFont systemFontOfSize:25.0]; _nextBtn.tintColor = [UIColor whiteColor]; [_nextBtn setTitle:@"Next" forState:UIControlStateNormal]; [_nextBtn addTarget:self action:@selector(nextBtn:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.textField]; [self.view addSubview:self.nextBtn]; // 注册监听者 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(ChangeTextFText:) name:@"ChangeValue" object:nil]; } -(void)ChangeTextFText:(NSNotification *)notification{ NSDictionary *dict = notification.userInfo; NSLog(@"dict = %@",dict); _textField.text = dict[@"Value"]; //移除通知 [[NSNotificationCenter defaultCenter]removeObserver:self]; } //进入第二层界面即传值界面 -(void)nextBtn:(UIButton *)sender{ SecondViewController *secondVC = [[SecondViewController alloc]init]; [self presentViewController:secondVC animated:YES completion:nil]; } @end
第二界面
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35#import "SecondViewController.h" @interface SecondViewController () @property (nonatomic,strong) UITextField *textSecond; @property (nonatomic,strong) UIButton *backBtn; @end @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [super viewDidLoad]; self.view.backgroundColor = [UIColor cyanColor]; //创建textField _textSecond = [[UITextField alloc]initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 50)]; _textSecond.borderStyle = UITextBorderStyleRoundedRect; _textSecond.backgroundColor = [UIColor whiteColor]; //创建button _backBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; _backBtn.frame = CGRectMake(20, 200, self.view.bounds.size.width-40,40); _backBtn.backgroundColor = [UIColor blueColor]; _backBtn.tintColor = [UIColor whiteColor]; _backBtn.titleLabel.font = [UIFont systemFontOfSize:25.0]; [_backBtn setTitle:@"Back" forState:UIControlStateNormal]; [_backBtn addTarget:self action:@selector(backBtn:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.textSecond]; [self.view addSubview:self.backBtn]; } -(void)backBtn:(UIButton *)sender{ // 注册通知 [[NSNotificationCenter defaultCenter]postNotificationName:@"ChangeValue" object:nil userInfo:@{@"Value":_textSecond.text}]; NSLog(@"%@",self.textSecond.text); [self dismissViewControllerAnimated:YES completion:nil]; } @end
最后
以上就是疯狂悟空最近收集整理的关于【iOS】通知传值的全部内容,更多相关【iOS】通知传值内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复