【iOS】通知传值

通知传值与协议传值都是反向的传值方式,即从第二个界面传到第一个界面。

首先确定传值界面需要做的:

  • 传值的发起者是第二层界面,第二层界面作为传值的发起者,需要明确它是要注册通知

注册通知:

//ChangeValue即为通知名称,需要与注册监听者中的通知名称保持一致
//object:需要与注册监听者中的object保持一致,除非注册监听者中的object为nil。
//userInfo:信息字典,包含了有关通知的可选信息
[[NSNotificationCenter defaultCenter]postNotificationName:@"ChangeValue" object:nil userInfo:@{@"Value":_textF.text}];

然后确定接收值界面需要做的:

  • 传值的接收者是第一层界面,第一层界面作为传值的接收者,需要明确它是要注册监听者,同时在监听到通知时执行一个我们需要的方法,并且在通知发送完之后进行移除

注册监听者:

//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];

需要的方法:
利用这个自定义方法来完成想要传的各种值

移除:

//写在接收值界面也是第一界面,在完成传值后进行移除通知,防止内存泄露 
[[NSNotificationCenter defaultCenter]removeObserver:self];

第一界面:

#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

第二界面

#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值