iOS中NSNotification浅析

来源:互联网 发布:csgo枪支数据 编辑:程序博客网 时间:2024/05/01 17:58

       在iOS开发中有一个“Notification Center”的概念。这是一个单例对象,允许当事件发生时通知一些对象。它允许我们在低耦合的情况下,满足控制器与一个任意的对象进行通信的目的。

       对于一个发出的通知,多个对象能够做出反应,即一对多的方式实现简单。实现一个Notification的步骤如下:

(1)注册通知:addObserver:selector:name:object,并实现触发通知后要实现的操作。

- (void)viewDidLoad {  [super viewDidLoad];  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadJpushData:) name:@"Mynotification"object:nil];}- (void)reloadJpushData: (NSNotification *)sender{  //触发通知后执行的操作;}

(2)发送通知:postNotificationName:object

 [[NSNotificationCenter defaultCenter] postNotificationName:@"Mynotification"object:nil];

(3)移除通知:removeObserver:和removeObserver:name:object:

移除某个指定的通知:

 [[NSNotificationCenter defaultCenter] removeObserver:self name:@"Mynotification" object:self];

移除所有通知:

[[NSNotificationCenter defaultCenter] removeObserver:self];


我现在通过一个Demo来演示如何使用Notification来进行界面间的数据传递。实例代码上传至:https://github.com/chenyufeng1991/iOS-Notification   。

(1)我这里在进行界面返回的时候传递数据,第一个界面的实现如下:

#import "ViewController.h"#import "inputValue.h"@interface ViewController()//使用UITextField返回界面时显示数据;@property (weak, nonatomic) IBOutlet UITextField *textShowValue;@end@implementation ViewController- (void)viewDidLoad{  [super viewDidLoad];  //在这里注册一个Notification,该Notification的名字为Mynotification,回调方法为displayValue;  [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(displayValue:) name:@"Mynotification" object:nil];}-(void)displayValue:(NSNotification*)value{  //显示该数据;  NSString *str=[value object];  self.textShowValue.text = str;  NSLog(@"%@",str);}//按钮点击跳转- (IBAction)openBtn:(id)sender {  //界面跳转;  inputValue *input = [[inputValue alloc] init];  [self presentViewController:input animated:true completion:nil];}@end

(2)第二个界面的实现如下,在这里发送一个通知并传递数据:

#import "inputValue.h"#import "ViewController.h"@interface inputValue()@property (weak, nonatomic) IBOutlet UITextField *textInput;@end@implementation inputValue#pragma mark - View lifecycle- (void)viewDidLoad{  [super viewDidLoad];}- (IBAction)returnBtn:(id)sender {  //在这里发送一个通知,Mynotification这里的名字要和前面注册通知的名字吻合。  [[NSNotificationCenter defaultCenter]postNotificationName:@"Mynotification" object:self.textInput.text];  [self dismissViewControllerAnimated:true completion:nil];}@end

(3)运行效果如下:

第二个界面传递数据:



第一个界面接收数据:



      总结,使用Notification传递消息是一对多的关系。我会在接下去继续对Notification的使用进行讲解。



github主页:https://github.com/chenyufeng1991  。欢迎大家访问!

1 1
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 宝宝只吃乳头不吃奶瓶怎么办 给泰迪奶狗喂奶呛到了怎么办 奶壶喂奶呛到了怎么办 宝宝喝奶瓶老是呛到怎么办 两个月宝宝睡偏头了怎么办 婴儿吐奶豆花状怎么办 吃母乳乳头破了怎么办 顺产后仰卧睡了怎么办 宝宝含着母乳睡怎么办 刚出生的孩子不吃母乳怎么办 月子里挤奶手痛怎么办 做完月子之后腿疼腰疼怎么办 腰疼引起的腿疼怎么办 上网上久了脑袋痛怎么办 莲花坐的脚麻怎么办 月子腿疼膝盖疼怎么办 做月子腿着凉了怎么办 出月子大腿根酸怎么办 出了月子腰酸痛怎么办 出了月子腿没劲怎么办 生完孩子后缺钙怎么办 生完孩子腿疼怎么办 生完孩子后腿疼怎么办 生完孩子肛门突出怎么办 生完孩子肋骨突出怎么办 蛙跳理蛙跳后腿疼怎么办 蛙跳两天后腿还疼怎么办 莲花菩提盘黑了怎么办 体育课蛙跳后肌肉拉伤怎么办 o型腿骨头弯了怎么办 小孩钢琴坐姿不对向后仰怎么办 小孩皮肤不好容易留疤怎么办 学游泳时站不稳怎么办 水呛到了不停打嗝怎么办 来月经前游泳了怎么办 快来完事游泳了怎么办 游泳时来月经了怎么办 经期第7天游泳了怎么办 来月经已经游了泳怎么办 月经来了要游泳怎么办 三个月宝宝趴着不会抬头怎么办