通知中心

来源:互联网 发布:买衣服淘宝真的不能买 编辑:程序博客网 时间:2024/04/27 13:34

IOS中通知中心NSNotificationCenter应用总结

一、了解几个相关的类

1、NSNotification

这个类可以理解为一个消息对象,其中有三个成员变量。

这个成员变量是这个消息对象的唯一标识,用于辨别消息对象。

@property (readonlycopyNSString *name;


这个成员变量定义一个对象,可以理解为针对某一个对象的消息。

@property (readonlyretainid object;


这个成员变量是一个字典,可以用其来进行传值。

@property (readonlycopyNSDictionary *userInfo;


NSNotification的初始化方法:

- (instancetype)initWithName:(NSString *)name object:(id)object userInfo:(NSDictionary *)userInfo;

+ (instancetype)notificationWithName:(NSString *)aName object:(id)anObject;


+ (instancetype)notificationWithName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;


注意:官方文档有明确的说明,不可以使用init进行初始化

2、NSNotificationCenter

这个类是一个通知中心,使用单例设计,每个应用程序都会有一个默认的通知中心。用于调度通知的发送的接受。

添加一个观察者,可以为它指定一个方法,名字和对象。接受到通知时,执行方法。

- (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;


发送通知消息的方法

- (void)postNotification:(NSNotification *)notification;

- (void)postNotificationName:(NSString *)aName object:(id)anObject;

- (void)postNotificationName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;


移除观察者的方法

- (void)removeObserver:(id)observer;

- (void)removeObserver:(id)observer name:(NSString *)aName object:(id)anObject;


几点注意:

1、如果发送的通知指定了object对象,那么观察者接收的通知设置的object对象与其一样,才会接收到通知,但是接收通知如果将这个参数设置为了nil,则会接收一切通知。

2、观察者的SEL函数指针可以有一个参数,参数就是发送的死奥西对象本身,可以通过这个参数取到消息对象的userInfo,实现传值。


二、通知的使用流程

首先,我们在需要接收通知的地方注册观察者,比如:

?
1
2
3
4
    //获取通知中心单例对象
    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    //添加当前类对象为一个观察者,name和object设置为nil,表示接收一切通知
    [center addObserver:self selector:@selector(notice:) name:@"123" object:nil];

之后,在我们需要时发送通知消息

?
1
2
3
4
    //创建一个消息对象
    NSNotification * notice = [NSNotification notificationWithName:@"123" object:nil userInfo:@{@"1":@"123"}];
    //发送消息
       [[NSNotificationCenter defaultCenter]postNotification:notice];

我们可以在回调的函数中取到userInfo内容,如下:

?
1
2
3
-(void)notice:(id)sender{
    NSLog(@"%@",sender);
}

打印结果如下:






IOS NSNotificationCenter 通知中心的使用

  通知中心,它是IOS程序内部的一种消息广播机制,通过它,可以实现无引用关系的对象之间的通信。通知中心他是基于观察者模式,它只能进行程序内部通信,不能跨应用程序进程通信。当通知中心接受到消息后会根据设置,将消息发送给订阅者,这里的订阅者可以有多个。

  通知中心与代理模式类似,都可以实现多个对象间通信,通知中心可以将一个通知发送给多个监听者,而代理模式每个对象只能添加一个代理。但无论是那种模式,都是一种低耦合的设计,实现对象间的通信。

使用通知中心的步骤

1、注册观察者对某个事件(以字符串命名)感兴趣,并设置该事件触发时执行的Selector或Block

2、NSNotificationCenter在某个时机激发事件(以字符串命名)

3、观察者在收到感兴趣的事件时,执行相应地Selector或Block

4、移除通知

通知中心案例

  利用导航添加三个界面,在第三个界面上添加一组按钮,当点击按钮的时候,设置当前页面背景色为按钮颜色,并发送通知,将前面两个界面背景色也设置为选择的颜色,程序框架和界面如图所示

(1)注册通知,在界面三的viewDidLoad方法中,为界面一和界面二注册通知,当发送通知的时候,会去界面一和界面二中调用对应的方法

界面三中代码:

- (void)viewDidLoad {

    [super viewDidLoad];

   

    SecondViewController *secondVC =[self.navigationController.viewControllers objectAtIndex:1];

    ViewController *firstVC =[self.navigationController.viewControllers firstObject];

    //注册通知

    [[NSNotificationCenterdefaultCenter] addObserver:secondVC selector:@selector(changeBgColor:)name:kNotificationNameobject:nil];

    [[NSNotificationCenterdefaultCenter] addObserver:firstVC selector:@selector(changeBgColor:)name:kNotificationNameobject:nil];

}

  ps:kNotificationName 是通过宏定义定义的字符串

  (2)    在界面三点击颜色按钮时,换背景色,并且给界面一和界面二发送通知

界面三中代码:

- (IBAction)chooseBgColor:(UIButton *)sender {

 

    NSArray *colorArray = @[[UIColorredColor],[UIColor blueColor],[UIColor greenColor],[UIColorpurpleColor],[UIColor whiteColor],[UIColor blackColor],[UIColororangeColor],[UIColor yellowColor],[UIColor brownColor]];

    self.view.backgroundColor =[colorArray objectAtIndex:sender.tag-1];

    //引发通知

    [[NSNotificationCenterdefaultCenter] postNotificationName:kNotificationNameobject:[colorArray objectAtIndex:sender.tag-1]];

 

}

  (3)在界面一和界面二中处理通知,更改界面背景色

界面一和界面二中代码:

-(void)changeBgColor:(NSNotification*)notification

{

    [self.viewsetBackgroundColor:notification.object];

}

  (4)在界面三移除通知,移除通知一般在重写父类的dealloc方法,但是arc方式下,在dealloc方法中不能调用父类的dealloc方法

界面三中代码:

-(void)dealloc

{

    [[NSNotificationCenterdefaultCenter] removeObserver:kNotificationName];

}

 

 


0 0
原创粉丝点击