iOS 通知中心 NSNotificationCenter & NSNotification

通知中心是 Foundation 框架的一个子系统,它向应用程序中注册为某个事件观察者的所有对象广播消息(即通知)。(从编程角度而言,它是 NSNotificationCenter 类的实例)。该事件可以是发生在应用程序中的任何事情,例如进入后台状态,或者用户开始在文本栏中键入。通知是告诉观察者,事件已经发生或即将发生,因此让观察者有机会以合适的方式响应。通过通知中心来传播通知,是增加应用程序对象间合作和内聚力的一种途径。


任何对象都可以观察通知,但要做到这一点,该对象必须注册,以接收通知。在注册时,它必须指定选择器,以确定由通知传送所调用的方法;方法签名必须只有一个参数:通知对象。注册后,观察者也可以指定发布对象。

(以上是官方文档中的解释)

------------------------------------------华丽的分割线----------------------------------------------------------

通知中心包括两个重要的类:

(1)NSNotificationCenter: 实现NSNotificationCenter的原理是一个观察者模式,获得NSNotificationCenter的方法只有一种,那就是[NSNotificationCenter defaultCenter] ,通过调用静态方法defaultCenter就可以获取这个通知中心的对象了,而NSNotificationCenter是一个单例模式,而这个通知中心的对象会一直存在于一个应用的生命周期。

  (2) NSNotification: 这是消息携带的载体,通过它,可以把消息内容传递给观察者。

 (3)一个NSNotificationCenter可以有许多的通知消息NSNotification,对于每一个NSNotification可以有很多的观察者Observer来接收通知。

通过上面的介绍可以知道,通过通知中心也可以实现不同类之间的参数传递。

注意当接受到消息后,不想再收到消息了,要把observer删除remove。

下面介绍如何使用(具体解释看文档)。

(1)NSNotification :用于创建传递的消息

Creating Notifications
+ notificationWithName:object:
+ notificationWithName:object:userInfo:
Getting Notification Information
– name
– object
– userInfo


(2) NSNotificationCenter :用于发送消息

Getting the Notification Center
+ defaultCenter
Managing Notification Observers
– addObserverForName:object:queue:usingBlock:
– addObserver:selector:name:object:
– removeObserver:
– removeObserver:name:object:
Posting Notifications
– postNotification:
– postNotificationName:object:
– postNotificationName:object:userInfo:

demo(例子中基本上涉及到以上所有的方法了):

定义了两个类:Poster(发送消息)和Observer(接受消息)

Poster.h

#import <Foundation/Foundation.h>

@interface Poster : NSObject

-(void)postMessage;

@end

Poster.m

#import "Poster.h"

@implementation Poster

-(void)postMessage{
    
    //1.下面两条语句等价
    //二者的区别是第一条语句是直接发送消息内容,第二条语句先创建一个消息,然后再发送消息
    [[NSNotificationCenter defaultCenter] postNotificationName:@"PosterOne" object:@"This is posterone!"];
    
//    [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"PosterOne" object:@"This is posterone"]];
    
    //2.下面两条语句等价
    //参数:userInfo  --- Information about the the notification.
    [[NSNotificationCenter defaultCenter] postNotificationName:@"PosterTwo" object:@"This is postertwo" userInfo:[NSDictionary dictionaryWithObject:@"value" forKey:@"key"]];
    
//    [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"PosterTwo" object:@"This is postertwo" userInfo:[NSDictionary dictionaryWithObject:@"value" forKey:@"key"]]];
}

@end

Observer.h

#import <Foundation/Foundation.h>

@interface Observer : NSObject

-(void)observer;

@end

Observer.m

#import "Observer.h"

@implementation Observer

-(void)observer {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callBack1:) name:@"PosterOne" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callBack2:) name:@"PosterTwo" object:nil];
    
    //删除所有的observer
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
    //删除名字为name的observer
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"PosterOne" object:nil];
    
}

-(void)callBack1:(NSNotification*)notification{
    NSString *nameString = [notification name];
    NSString *objectString = [notification object];
    NSLog(@"name = %@,object = %@",nameString,objectString);
}

-(void)callBack2:(NSNotification*)notification{
    NSString *nameString = [notification name];
    NSString *objectString = [notification object];
    NSDictionary *dictionary = [notification userInfo];
    NSLog(@"name = %@,object = %@,userInfo = %@",nameString,objectString,[dictionary objectForKey:@"key"]);
}

@end

main.m

#import <Foundation/Foundation.h>
#import "Poster.h"
#import "Observer.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        //注意这里的顺序,要先observer,然后再poster
        Observer *myObserver = [[Observer alloc] init];
        [myObserver observer];
        
        Poster *poster = [[Poster alloc] init];
        [poster postMessage];
    }
    return 0;
}

好了,大概有关的内容都差不多了吧 大笑

附:

不过有个方法

addObserverForName:object:queue:usingBlock:

还不太懂如何使用,暂时放一下,有知道了麻烦评论告诉了。
  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
iOS 中,可以使用通知NSNotification)机制来实现组件间的解耦和消息传递。通知是一种广播机制,一个组件可以发布(post)一个通知,其他组件可以订阅(observe)这个通知,并在接收到通知时执行相应的操作。下面是一个通知的实例说明。 假设我们有一个应用程序,在其中有两个视图控制器 AViewController 和 BViewController。AViewController 中有一个按钮,当用户点击按钮时,我们希望 BViewController 接收到一个通知,并在接收到通知时更新界面。 首先,在 BViewController 中,我们需要注册一个通知观察者,代码如下: ``` - (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@&quot;MyNotification&quot; object:nil]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)handleNotification:(NSNotification *)notification { // 处理通知 NSLog(@&quot;Received notification: %@&quot;, notification); } ``` 在这里,我们使用 defaultCenter 对象注册了一个观察者,指定了通知的名称为 MyNotification。当通知被发布时,handleNotification 方法会被调用,并且可以在方法中处理通知。 接下来,在 AViewController 中,我们需要发布一个通知,代码如下: ``` - (IBAction)postNotification:(id)sender { [[NSNotificationCenter defaultCenter] postNotificationName:@&quot;MyNotification&quot; object:nil userInfo:@{@&quot;key&quot;: @&quot;value&quot;}]; } ``` 在这里,我们使用 defaultCenter 对象发布了一个通知,指定了通知的名称为 MyNotification,没有指定通知的发送者,同时可以携带一些额外的信息,例如 userInfo 字典中的键值对。 当用户点击 AViewController 中的按钮时,就会触发 postNotification 方法,同时 BViewController 中的 handleNotification 方法也会被调用,并且可以在其中处理通知。 总之,通知机制是一种非常方便的组件间通信方式,可以实现解耦和消息传递。在使用通知时,需要注意及时注册和注销观察者,并且指定通知的名称和携带的信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值