通知中心(NSNotificationCenter)实际是在程序内部提供了一种广播机制。把接收到的消息,根据内部的消息转发表,将消息转发给需要的对象。这句话其实已经很明显的告诉我们要如何使用通知了。第一步:在需要的地方注册要观察的通知,第二步:在某地方发送通知。(这里注意:发送的通知可能是我们自定义的,也可能是系统的)。

一,使用通知

第1中创建通知方法

//注意,通知的使用是有先后顺序的

//一定要先监听通知,然后在发送通知

 //第一种方法
// 添加一个通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addNotification) name:@"tongzhi" object:nil]; //发送一个通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"tongzhi" object:nil];
  //发送通知也可以传递一些参数
[NSNotificationCenter defaultCenter] postNotificationName:<#(nonnull NSNotificationName)#> object:<#(nullable id)#> userInfo:<#(nullable NSDictionary *)#>

可以在监听的通知的方法获取通知的信息

- (void)addNotification {

    NSLog(@"接受通知");
}

最后要移除通知

- (void)dealloc {

    //移除观察者 self
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

第2种通知的使用使用 block 比较方便简单

这个方法需要一个id类型的值接受

@property (nonatomic, weak) id observe;
    //第二种方法
//Name: 通知的名称
//object:谁发出的通知
//queue: 队列,决定 block 在哪个线程中执行, nil 在发布通知的线程中执行
//usingBlock: 只要监听到通知,就会执行这个 block
//这个通知返回一个 id 这个通知同样需要移除
_observe = [[NSNotificationCenter defaultCenter] addObserverForName:@"tongzhi" object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
NSLog(@"收到了通知");
}]; [[NSNotificationCenter defaultCenter] postNotificationName:@"tongzhi" object:nil];

同样,这里也需要移除通知,但是这里的观察者不是 self 而是 _observe

- (void)dealloc {

    //移除观察者 _observe
[[NSNotificationCenter defaultCenter] removeObserver:_observe];
}

二 .通知在多线程中的使用

通知在多线程中使用

//通知在接收的方法跟发送通知所在的线程中一样

异步发送通知, 主线程监听通知, 接收通知的方法在子线程中

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    //发送通知
dispatch_async(dispatch_get_global_queue(, ), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"tongzhi" object:nil];
});
} - (void)viewDidLoad {
[super viewDidLoad]; //多线程中使用通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addNotification) name:@"tongzhi" object:nil]; } - (void)addNotification {
NSLog(@"接受通知");
NSLog(@"%@",[NSThread currentThread]); //主线程:监听通知,异步发送通知
//总结: 接受通知的代码由发出通知的线程
//更新 UI
dispatch_sync(dispatch_get_main_queue(), ^{
//更新 UI
});
}

控制台输出 : 异步

-- ::05.537 通知多线程使用[:] 接受通知
-- ::05.538 通知多线程使用[:] <NSThread: 0x60800026d8c0>{number = , name = (null)}

主线程中发送通知,异步线程中监听通知

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    //主线程中发送通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"tongzhi" object:nil];
} - (void)viewDidLoad {
[super viewDidLoad]; //多线程中使用通知 //异步监听通知 dispatch_async(dispatch_get_global_queue(, ), ^{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addNotification) name:@"tongzhi" object:nil];
});
} - (void)addNotification {
NSLog(@"接受通知");
NSLog(@"%@",[NSThread currentThread]); //异步:监听通知,主线程发送通知
//总结: 接受通知的代码由发出通知的线程 }

控制台输出

-- ::40.160 通知多线程使用[:] 接受通知
-- ::40.160 通知多线程使用[:] <NSThread: 0x600000072ac0>{number = , name = main}

使用 block 创建通知的方法,

queue :[NSOperationQueue mainQueue] 这样都会在主线程中执行block 中代码
    _observe = [[NSNotificationCenter defaultCenter] addObserverForName:@"tongzhi" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
NSLog(@"收到了通知");
}];

控制台输出 : 主线程中执行

-- ::39.277 通知多线程使用[:] 收到了通知
-- ::39.277 通知多线程使用[:] <NSThread: 0x60000006ea40>{number = , name = main}

iOS中通知中心NSNotificationCenter应用总结的更多相关文章

  1. IOS中通知中心(NSNotificationCenter)

    摘要 NSNotification是IOS中一个调度消息通知的类,采用单例模式设计,在程序中实现传值.回调等地方应用很广.   IOS中通知中心NSNotificationCenter应用总结 一.了 ...

  2. iOS中通知中心(NSNotificationCenter)的使用总结

    一.了解几个相关的类 1.NSNotification 这个类可以理解为一个消息对象,其中有三个成员变量. 这个成员变量是这个消息对象的唯一标识,用于辨别消息对象. @property (readon ...

  3. iOS基础 - 通知中心(NSNotificationCenter)

    通知中心(NSNotificationCenter) 每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信 任何一个对象都可以向通知中心发 ...

  4. iOS中 通知中心Text (实例)

    指定根视图 self.window.rootViewController = [RootViewController new]; 方法实现: #import "RootViewControl ...

  5. IOS Notification 通知中心

    1.     通知中心概述 通知中心实际上是在程序内部提供了消息广播的一种机制.通知中心不能在进程间进行通信.实际上就是一个二传手,把接收到的消息,根据内部的一个消息转发表,来将消息转发给需要的对象. ...

  6. iOS 通知中心 NSNotificationCenter

    iOS开发中,每个app都有一个通知中心,通知中心可以发送和接收通知. 在使用通知中心 NSNotificationCenter之前,先了解一下通知 NSNotification. NSNotific ...

  7. iOS开发之通知中心(NSNotificationCenter)

    前言 面向对象的设计思想是把行为方法封装到每一个对象中,以用来增加代码的复用性.正是这种分散封装,增加了对象之间的相互关联,总是有很多的对象需要彼此了解以及相互操作! 一个简单示例说明这种交互产生的对 ...

  8. 通知中心NSNotificationCenter的使用

    通知中心NSNotificationCenter的使用 Cocoa框架中,通知中心以及KVO都属于设计模式中的观察者. Source 在使用通知中心之前,对通知中心类进行了简单的封装,以便可读性更强. ...

  9. iOS中通知传值

    NSNotification 通知中心传值,可以跨越多个页面传值, 一般也是从后面的页面传给前面的页面.   思路: 第三个界面的值传给第一个界面. 1. 在第一个界面建立一个通知中心, 通过通知中心 ...

随机推荐

  1. windows下php cli模式,提示出错

    搞一下php cli,一直提示,找不到php_pdo_mssql.dll模块 原来是在php.ini加载了该模块,但ext下又没有该模块 即便下载了一个php_pdo_mssql.dll,但还是会提示 ...

  2. 关于缺省路由传递问题的探讨(上)[ip default-network、ip default-gateway等]

    指定缺省路由(last resort gateway)的指令供有3种,可以分成两类:   1.Ip default-gateway   仅在 "no ip routing" (关闭 ...

  3. CCS5 编译器手动设置dsp支持可变参数宏等问题

    IDE:CSS5.4,compiler不支持可变参数宏.需要手动设置编译器相关选项: Language Option->Language Mode —>no strict ANSI. 1. ...

  4. SharePoint 2013的100个新功能之社交

    一:社会能力 SharePoint 2013引入了一个新东西叫做社会能力,使公司组织中的用户社会化协作.我的网站难以置信地做了改进以集成社会能力.除了我的网站,新的社区网站(新闻提要),关注用户和关注 ...

  5. Codeforces 600 E. Lomsat gelral (dfs启发式合并map)

    题目链接:http://codeforces.com/contest/600/problem/E 给你一棵树,告诉你每个节点的颜色,问你以每个节点为根的子树中出现颜色次数最多的颜色编号和是多少. 最容 ...

  6. Cipher Message

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=34121#problem/C // File Name: c.cpp // Author: ...

  7. C:函数

    函数 函数:都是实现一定的功能.具有特定功能的代码段.凡是由系统提供的函数就是库函数,自己写的函数就是自定义函数. 如何定义一个函数  :  函数类型修饰符  函数名 (函数参数)  {  函数语句 ...

  8. CString和string的互相转换

    CString->std::string 例子: CString strMfc=“test“; std::string strStl; strStl=strMfc.GetBuffer(0); s ...

  9. Sublime Text3 激活教程

    Sublime Text3激活 在使用Sublime时会定期弹出购买提示框,避免出现购买提示,影响工作效率,我们可以使用网上的激活码,虽然有些不厚道,但是工作以后,一定选择购买正版支持一下. 打开Su ...

  10. 用DataBaseMail发图片并茂的邮件

    不知道各位的老板有没有这样的要求, 一些系统中的数据需要定时发出邮件提醒, 如呆料就要到期或者一些待办的事项提醒. 当然这些用SSRS报表订阅可以实现,但有些公司没有设定相应的报表服务,又或者只是一些 ...