UI学习:通知传值
文章目录通知传值核心概念什么是通知中心三个核心角色通知的组成通知的生命周期举例讲解通知发送的对象通知传值通知传值是 iOS 开发中一种解耦的传值方式它允许没有直接引用关系的对象之间进行通信。核心概念什么是通知中心NSNotificationCenter是一个单例对象负责管理通知的发送和接收。它像一个广播站发送者发布通知不需要知道谁在监听接收者监听通知不需要知道谁在发送通知中心负责转发三个核心角色角色说明对应方法通知对象携带信息的载体NSNotification观察者监听通知的对象addObserver:selector:name:object:发布者发送通知的对象postNotificationName:object:userInfo:通知的组成NSNotification 包含三个部分 - name: 通知名称唯一标识 - object: 发送通知的对象通常是 self - userInfo: 传递的数据字典通知的生命周期观察者注册 → 2. 发送通知 → 3. 通知中心分发 → 4. 观察者回调 → 5. 移除观察者举例讲解VCSecond 有一个 TextField输入文字后通过通知传给 VCFirst 的 Label 显示创建VCFirst 和 VCSecond 两个视图控制器给VCFirst 定义属性showLahbel用来显示传值的结果, 定义button 来通过调用事件切换视图控制器定义通知的名称VCFirsrt.m// 定义通知名称staticNSString*constkTextFieldNotificationTextFieldNotification;interfaceVCFirst()property(nonatomic,strong)UILabel*showLabel;property(nonatomic,strong)UIButton*pushButton;end定义通知名称的作用:通知名称就像一个频道号或广播频率决定了通知发送方和接收方能否匹配上。创建VCFirst 的Label 和 UIButton, 并注册通知监听// VCFirst.m-(void)viewDidLoad{[superviewDidLoad];self.view.backgroundColor[UIColor whiteColor];self.titleFirst VC;// 创建 Label用于显示接收的数据self.showLabel[[UILabel alloc]initWithFrame:CGRectMake(50,250,300,50)];self.showLabel.text等待接收文字...;self.showLabel.textAlignmentNSTextAlignmentCenter;self.showLabel.backgroundColor[UIColor lightGrayColor];self.showLabel.textColor[UIColor blackColor];[self.view addSubview:self.showLabel];// 创建按钮跳转到 Second VCself.pushButton[UIButton buttonWithType:UIButtonTypeSystem];self.pushButton.frameCGRectMake(100,350,150,44);[self.pushButton setTitle:去输入文字forState:UIControlStateNormal];[self.pushButton addTarget:selfaction:selector(pushToSecond)forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:self.pushButton];// 注册通知监听[[NSNotificationCenter defaultCenter]addObserver:selfselector:selector(receiveText:)name:kTextFieldNotification object:nil];}[NSNotificationCenter defaultCenter] addObserver:self selector:selector(receiveText:) name:kTextFieldNotification object:nil的作用:这是向通知中心注册一个观察者的方法调用包含 4 个关键参数参数类型作用本例中的值observerid观察者对象谁要监听通知self当前对象selectorSEL收到通知后调用哪个方法selector(receiveText:)nameNSString监听哪个通知名称kTextFieldNotificationobjectid限定发送者对象过滤器nil不限定设置按钮事件, 创建视图控制器VCSecond 并且切换视图控制器// VCFirst.m-(void)pushToSecond{VCSecond*secondVC[[VCSecond alloc]init];[self.navigationController pushViewController:secondVC animated:YES];}接受通知的方法, 收到通知调用的方法, 将接受到的字符串赋值给laebl,显示出来// VCFirst.m// 接收通知的方法-(void)receiveText:(NSNotification*)notification{// 从 userInfo 中取出 textNSString*textnotification.userInfo[text];// 更新 Labelself.showLabel.text[NSString stringWithFormat:收到%,text];}在对象销毁的时候移除观察者, 否则会导致野指针// 移除观察者重要-(void)dealloc{[[NSNotificationCenter defaultCenter]removeObserver:self];}给VCSecond 定义属性inputTextField和属性 sendButton , 用来输入要传值的内容和切换视图控制器, 同时定义通知名称// VCSecond.m#importVCSecond.h// 通知名称必须和 First 中一致staticNSString*constkTextFieldNotificationTextFieldNotification;interfaceVCSecond()property(nonatomic,strong)UITextField*inputTextField;property(nonatomic,strong)UIButton*sendButton;end通知名称本质上是发送方和接收方之间约定的字符串 ,需要在两端保持一致, 才能实现通信因此,需要在需要发送和接受通知的视图控制器中都定义通知名称并且保持一致创建VCSecond 的inputField 和 sendButton// VCSecond.m-(void)viewDidLoad{[superviewDidLoad];// Do any additional setup after loading the view.self.view.backgroundColor[UIColor whiteColor];self.titleVCSecond;// 创建TextFieldself.inputTextField[[UITextField alloc]initWithFrame:CGRectMake(50,250,275,44)];// 输入框风格self.inputTextField.borderStyleUITextBorderStyleRoundedRect;self.inputTextField.placeholder请输入文字;// 编辑时显示清除按钮self.inputTextField.clearButtonModeUITextFieldViewModeWhileEditing;[self.view addSubview:_inputTextField];// 创建发送按钮self.sendButton[UIButton buttonWithType:UIButtonTypeSystem];self.sendButton.frameCGRectMake(100,320,150,44);[self.sendButton setTitle:发送并返回forState:UIControlStateNormal];self.sendButton.backgroundColor[UIColor systemBlueColor];[self.sendButton setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];[self.sendButton addTarget:selfaction:selector(sendAndBack)forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:_sendButton];}设置按钮事件: 发送通知 并 切换视图控制器// VCSecond.m-(void)sendAndBack{// 获取输入的文字NSString*strself.inputTextField.text;// 如果文字为空, 给定默认值if(str.length0){str空消息;}// 发送通知[[NSNotificationCenter defaultCenter]postNotificationName:kTextFieldNotification object:selfuserInfo:{text:str}];// 返回上一个界面[self.navigationController popViewControllerAnimated:YES];}发送通知:[[NSNotificationCenter defaultCenter] postNotificationName: kTextFieldNotification object: self userInfo: {text: str}];这段代码实现了发送通知的功能[NSNotificationCenter defaultCenter]获取通知中心的单例,[NSNotificationCenter defaultCenter]: 通知的名称object: 发送者userInfo: 携带的数据, 这里是一个字典, 可以传递多个数据, 例如:// 传递多个数据userInfo:{text:str,color:[UIColor redColor],number:100}通知发送的对象发送的消息是NSNotification对象,// postNotificationName:object:userInfo: 方法签名-(void)postNotificationName:(NSNotificationName)name object:(nullable id)object userInfo:(nullable NSDictionary*)userInfo;// 参数说明// name: 通知名称字符串// object: 发送者对象任意对象// userInfo: 数据字典NSDictionary在接收消息的时候通过 userInfo 属性取字典// 通过 userInfo 属性取字典-(void)handleNotification:(NSNotification*)notification{NSString*textnotification.userInfo[text];}