[iOS]-通知传值

通知传值介绍

通知传值也是类似于协议传值这样的逆向传值,即B界面向A界面传值
谁需要监听值的变化(谁需要接收值),谁就注册通知,且通知的接收者必须先注册通知

通知传值过程

  1. 注册通知
  2. 通知中心发送通知消息,其中name(通知名)前后要保持一致性(否则无法实现通知过程)
  3. 实现通知内部的方法,并实现传值
  4. 消息发送完之后要移除通知

通知传值例子:

例如我们在A界面创建一个label和一个button,其中label用于在屏幕上显示从通知中接收到的值,button用于点击跳转到B界面,在B界面创建一个textField和一个button,其中textField用于输入所要传的字符串,button用于点击触发发送通知事件和移除当前界面显示A界面

在B界面的.h文件中:

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController
//用于输入所要传的字符串的textField
@property (nonatomic, strong) UITextField *textField;
//用于点击触发发送通知事件和移除当前界面显示A界面的button
@property (nonatomic, strong) UIButton *button;

@end

在B界面的.m文件中:

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    _button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [_button setTitle:@"返回" forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(pressButton) forControlEvents:UIControlEventTouchUpInside];
    _button.layer.cornerRadius = 8.0;
    _button.layer.masksToBounds = YES;
    _button.backgroundColor = [UIColor yellowColor];
    _button.frame = CGRectMake(174, 500, 80, 40);
    [self.view addSubview:_button];
    
    _textField = [[UITextField alloc] init];
    _textField.frame = CGRectMake(139, 350, 150, 50);
    _textField.layer.borderColor = [UIColor blackColor].CGColor;
    _textField.layer.borderWidth = 1;
    _textField.layer.cornerRadius = 8.0;
    _textField.layer.masksToBounds = YES;
    [self.view addSubview:_textField];
}

//按钮的事件函数
- (void) pressButton {
    //发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeLabel" object:nil userInfo:@{@"Text":_textField.text}];
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end

在A界面的.h文件中:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
//用于在屏幕上显示从通知中接收到的值的label
@property (nonatomic, strong) UILabel *label;
//用于点击跳转到B界面的button
@property (nonatomic, strong) UIButton *button;

@end

在A界面的.m文件中:

#import "ViewController.h"
#import "SecondViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    _button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [_button setTitle:@"跳转" forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(pressButton) forControlEvents:UIControlEventTouchUpInside];
    _button.layer.cornerRadius = 8.0;
    _button.layer.masksToBounds = YES;
    _button.backgroundColor = [UIColor yellowColor];
    _button.frame = CGRectMake(174, 500, 80, 40);
    [self.view addSubview:_button];
    
    _label = [[UILabel alloc] init];
    _label.text = @"通知传值";
    _label.frame = CGRectMake(174, 350, 150, 30);
    [self.view addSubview:_label];
    
    //注册监听者
    //addObserver 注册监听者,一般都是控制器本身去监听一个通知
    //selector 当监听到通知的时候执行的方法
    //name 通知的名字,要和发送的通知的对象的名字一致
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeLabel:) name:@"changeLabel" object:nil];
    
}

//按钮的事件函数
- (void) pressButton {
    SecondViewController *secondViewController = [[SecondViewController alloc] init];
    secondViewController.modalPresentationStyle = UIModalPresentationFullScreen;
    //跳转到第二个界面
    [self presentViewController:secondViewController animated:YES completion:nil];
}

//当监听到通知的时候执行的方法
- (void) changeLabel:(NSNotification *) notification {
    //用字典接收通知传过来的值
    NSDictionary *dictionary = notification.userInfo;
    //用接收到的值更新label的内容
    _label.text = dictionary[@"Text"];
}

//移除通知
- (void) dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end

运行结果:
初始的A界面:
请添加图片描述
点击跳转按钮后跳转到B界面并在B界面的textField中输入123456:
请添加图片描述
最后点击返回按钮后:
请添加图片描述
我们可以看到A界面的label中的内容变为了我们通知中传过来的值,至此我们的通知传值就全部完成了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值