iOS多界面传值之--通知传值

来源:互联网 发布:职场鸡汤 知乎 编辑:程序博客网 时间:2024/05/22 11:54

通知传值

通知传值也是逆向传值的一种,即第二界面向第一界面传值
谁要监听值的变化,谁就注册通知 特别要注意,通知的接受者必须存在这一条件
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.模拟器运行效果如下:
这里写图片描述

这里写图片描述

这里写图片描述

0 0