通知傳值
通知傳值也是逆向傳值的一種,即第二界面向第一界面傳值
誰要監聽值的變化,誰就註冊通知 特別要注意,通知的接受者必須存在這一條件
1.註冊通知
2.通知中心發送通知消息,其中name(通知名)前後要保持一致性
3.實現通知內部的方法,並實現傳值
4.消息發送完之後,要移除通知
5.新建一個繼承與UIViewController的SecondViewController,然後在.m文件中分別聲明UITextField 和 UIButton 這兩個屬性.再用懶加載的方法分別創建UITextField 和 UIbutton
#import "SecondViewController.h"
@interface SecondViewController ()
@property (nonatomic,strong) UITextField *textF;
@property (nonatomic,strong) UIButton *backBtn;
@end
@implementation SecondViewController
-(UITextField *)textF{
if (!_textF) {
_textF = [[UITextField alloc]initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 50)];
_textF.borderStyle = UITextBorderStyleRoundedRect;
_textF.backgroundColor = [UIColor whiteColor];
}
return _textF;
}
-(UIButton *)backBtn{
if (!_backBtn) {
_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];
}
return _backBtn;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor cyanColor];
[self.view addSubview:self.textF];
[self.view addSubview:self.backBtn];
}
6.實現一下BUtton裏面的點擊事件的方法,並且在這個方法裏面進行發送通知消息
-(void)backBtn:(UIButton *)sender{
// 發送通知
[[NSNotificationCenter defaultCenter]postNotificationName:@"ChangeValue" object:nil userInfo:@{@"Value":_textF.text}];
NSLog(@"%@",self.textF.text);
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
7.回到ViewController.m中,導入一下頭文件.並且是聲明屬性,創建UItextField 和 UIbutton,在viewDidLoad裏面註冊監聽者…
#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()
@property (nonatomic,strong) UITextField *textField;
@property (nonatomic,strong) UIButton *nextBtn;
@end
@implementation ViewController
-(UITextField *)textField{
if (!_textField) {
_textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 50)];
_textField.borderStyle = UITextBorderStyleRoundedRect;
_textField.backgroundColor = [UIColor whiteColor];
}
return _textField;
}
-(UIButton *)nextBtn{
if (!_nextBtn) {
_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];
}
return _nextBtn;
}
- (void)viewDidLoad {
[super viewDidLoad];
/**
通知傳值(逆向傳值 --> 第二個界面傳值到第一個界面)
s
1、註冊通知
2、通知中心發送一條消息通知,其中name前後一定要一樣
3、實現通知中心內部的方法,並實現傳值
4、消息發送完,要移除掉。(頁面將要消失的時候)
*/
self.navigationItem.title = @"通知傳值";
self.view.backgroundColor = [UIColor yellowColor];
[self.view addSubview:self.textField];
[self.view addSubview:self.nextBtn];
// 註冊監聽者
/**
* addObserver 註冊監聽者,一般都是控制器本身去監聽一個通知
*
* selector 當監聽到通知的時候執行的方法
*
* name 通知的名字,要和發送的通知的對象的名字一致
*/
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(ChangeTextFText:) name:@"ChangeValue" object:nil];
}
8.實現Button的點擊事件方法和移除通知
-(void)ChangeTextFText:(NSNotification *)notification{
NSDictionary *dict = notification.userInfo;
NSLog(@"dict = %@",dict);
_textField.text = dict[@"Value"];
}
-(void)nextBtn:(UIButton *)sender{
SecondViewController *secondVC = [[SecondViewController alloc]init];
[self presentViewController:secondVC animated:YES completion:nil];
}
//移除通知
-(void)dealloc{
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
9.模擬器運行效果如下: