NSNotificationCenter 通知中心传值
1、NSNotification
这个类可以理解为一个消息对象,其中有三个成员变量。
这个成员变量是这个消息对象的唯一标识,用于辨别消息对象。
@property (readonly, copy) NSString *name;
这个成员变量定义一个对象,可以理解为针对某一个对象的消息。
@property (readonly, retain) id object;
这个成员变量是一个字典,可以用其来进行传值。
@property (readonly, copy) NSDictionary *userInfo;
几点注意:
1、如果发送的通知指定了object对象,那么观察者接收的通知设置的object对象与其一样,才会接收到通知,但是接收通知如果将这个参数设置为了nil,则会接收一切通知。
2、观察者的SEL函数指针可以有一个参数,参数就是发送的死奥西对象本身,可以通过这个参数取到消息对象的userInfo,实现传值。
首先我们要确认那边要传值那边要接受传过来的值,
在传值的一方我们要写一个创建一个消息 NSNotification ,并且用通知中心NSNotificationCenter 发送这个消息
接收传过来的值这里我们要创建一个通知中心NSNotificationCenter 并且添加观察者,在添加观察者这个方法里面有一个响应事件的方法 我们可以在响应事件的方法里面接收传过来的值
好啦我们开始写代码吧
传值的一方在这个控制器里面我们创建一个UITextFiled 把UITextfiled的text传到上一个控制器的UIlabel上显示出来
#import "PYJViewController.h" @interface PYJViewController ()
@property (nonatomic,strong) UITextField *textField;
@end @implementation PYJViewController - (void)viewDidLoad {
[super viewDidLoad];
UIButton * button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame =CGRectMake(, , , );
[button setTitle:@"返回" forState:UIControlStateNormal];
button.backgroundColor=[UIColor yellowColor];
[button addTarget:self action:@selector(backLastPage:) forControlEvents:UIControlEventTouchUpInside];
self.textField=[[UITextField alloc]init];
self.textField.frame=CGRectMake(, , , );
self.textField.borderStyle=UITextBorderStyleRoundedRect; [self.view addSubview:self.textField]; [self.view addSubview:button];
self.view.backgroundColor=[UIColor whiteColor]; } - (void)backLastPage:(UIButton *)bt{ //创建一个消息对象
NSNotification * notice =[NSNotification notificationWithName:@"notice" object:nil userInfo:@{@"text":self.textField.text}];
//发送消息
[[NSNotificationCenter defaultCenter]postNotification:notice]; [self.navigationController popViewControllerAnimated:YES]; }
接收传值的控制器:
#import "ViewController.h"
#import "PYJViewController.h"
@interface ViewController () @property (weak, nonatomic) IBOutlet UILabel *label;
- (IBAction)buttonAction:(UIButton *)sender;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; self.label.text=@"测试一"; //获取通知中心
NSNotificationCenter * center =[NSNotificationCenter defaultCenter]; //添加观察者 Observer表示观察者 reciveNotice:表示接收到的消息 name表示再通知中心注册的通知名 object表示可以相应的对象 为nil的话表示所有对象都可以相应
[center addObserver:self selector:@selector(reciveNotice:) name:@"notice" object:nil]; } - (void)reciveNotice:(NSNotification *)notification{ NSLog(@"收到消息啦!!!"); self.label.text=[notification.userInfo objectForKey:@"text"]; } - (IBAction)buttonAction:(UIButton *)sender { PYJViewController * vc=[[WBBViewController alloc]init]; [self.navigationController pushViewController:vc animated:YES]; }
NSNotificationCenter 通知中心传值的更多相关文章
- NSNotificationCenter通知中心
概述 NSNotificationCenter通知中心,通常用于一对一或者一对多的消息传递,即当一个地方改变时,要求改变其他的一些地方,例如当网络请求回来了新的数据,需要刷新本地信息和本地内存里面的界 ...
- iOS之NSNotificationCenter通知中心使用事项
其实这里的通知和之前说到的KVO功能很想,也是用于监听操作的,但是和KVO不同的是,KVO只用来监听属性值的变化,这个发送监听的操作是系统控制的,我们控制不了,我们只能控制监听操作,类似于Androi ...
- iOS 设计模式-NSNotificationCenter 通知中心
通知介绍 每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信 任何一个对象都可以向通知中心发布通知(NSNotification),描述 ...
- 通知中心 NSNotificationCenter 的简单使用方法
NSNotificationCenter(通知中心) [注意]需再dealloc中移除观察者 获取通知中心单例对象 NSNotificationCenter *center=[NSNotifi ...
- iOS 通知中心 NSNotificationCenter
iOS开发中,每个app都有一个通知中心,通知中心可以发送和接收通知. 在使用通知中心 NSNotificationCenter之前,先了解一下通知 NSNotification. NSNotific ...
- 通知中心 NSNotificationCenter
NSNotificationCenter 通知中心提供了一种在程序内广播信息的途径,一个NSNotificationCenter对象本质上是一个通知分发表(notification dispatch ...
- iOS中通知中心(NSNotificationCenter)的使用总结
一.了解几个相关的类 1.NSNotification 这个类可以理解为一个消息对象,其中有三个成员变量. 这个成员变量是这个消息对象的唯一标识,用于辨别消息对象. @property (readon ...
- IOS中通知中心(NSNotificationCenter)
摘要 NSNotification是IOS中一个调度消息通知的类,采用单例模式设计,在程序中实现传值.回调等地方应用很广. IOS中通知中心NSNotificationCenter应用总结 一.了 ...
- 通知中心NSNotificationCenter的使用
通知中心NSNotificationCenter的使用 Cocoa框架中,通知中心以及KVO都属于设计模式中的观察者. Source 在使用通知中心之前,对通知中心类进行了简单的封装,以便可读性更强. ...
随机推荐
- 配置zabbix通过微信报警企业微信报警
如今势态: 报警的方式可谓是八仙过海各显神通,如电话报警,短信报警,邮件报警,QQ报警,微信报警等等. 电话报警:一般都是使用别的平台的工具,平台给你提供一个接口供你使用,大多数为限量收费款 短信报警 ...
- ASP.NET的内置对象
Request 该对象用于检索从浏览器向服务器所发送的请求中的信息.在按下“提交”按钮时,Request对象将读取和提取通过HTTP请求发送的参数.在用户提交表单时,包含在输入控件中的数据将与表单 ...
- iOS日常学习 - 让你的 Xcode8 继续使用插件
本文转载,原文链接 随着 iOS10 的正式版即将发布,Xcode8 GM 也在发布会后放出,本文不会涉及到 Xcode8 有哪些更新,而是记录了如何让 Xcode8 继续支持 Plugin. 相信各 ...
- struts2——第一个案例
步骤如下 编程工具等百度云分享 1.创建一个web项目 2.引入struts2的基本jar包 struts2的基本jar包百度云 链接:https://pan.baidu.com/s/1LBnPJhF ...
- Android中获取资源的id和url方法总结
一,获取android工程里面的各种资源的id; 1.1 string型 比如下面: << string name=”OK”>> 客户端请求成功 << / stri ...
- BZOJ 3698 XWW的难题:有上下界的最大流
传送门 题意 给你一个 $ n*n $ 的正实数矩阵 $ A $ ,满足XWW性. 称一个 $ n*n $ 的矩阵满足XWW性当且仅当: $ A[n][n] = 0 $ 矩阵中每行的最后一个元素等于该 ...
- JSON Web Token (JWT) 简介
JSON Web Token (JWT) 是一种基于 token 的认证方案. JSON Web Token 的结构 一个 JWT token 看起来是这样的: eyJhbGciOiJIUzI1NiI ...
- 智课雅思词汇---二十三、名词性后缀mony
智课雅思词汇---二十三.名词性后缀mony 一.总结 一句话总结:Latin: action, result of an action or condition; a suffix that for ...
- Selenium with Python 001 - 安装篇
Selenium Python bindings 提供了一个简单的API,让你使用Selenium WebDriver来编写功能/校验测试. 通过Selenium Python的API,你可以非常直观 ...
- python 邮件发送实例
#!/usr/bin/env python # -*- coding: utf-8 -*- from email.header import Header from email.mime.text i ...