NSNotification通知类的使用

通过使用NSNotification通知类,可以实现一对多的传递关系,同时也能很好的避免类与类之间层层递进的关系。但是必须记得在使用过程中可能出现通知会被多次接收,并执行相关的方法,避免这种情况出现必须在接收通知前先移除要接收的通知。

 

步骤1 创建通知类的公共类

.h文件

 

//

//  NotificationManager.h

//  DemoNotification

//

//  Created by zhangshaoyu on 14-7-11.

//  Copyright (c) 2014年 zhangshaoyu. All rights reserved.

//  功能描述:通知模式使用公用类

 

#import <Foundation/Foundation.h>

 

// 定义通知名称

static NSString *const NameNotification = @"NameNotification";

 

@interface NotificationManager : NSObject

 

/// 发送通知

+ (void)notificationSendWithName:(NSString *)name userInfo:(id)infoObj;

 

/// 接收通知

+ (void)notificationReceiveWithName:(NSString *)name delegate:(id)delegate selector:(SEL)selector;

 

/// 移除通知

+ (void)notificationRemoveWithName:(NSString *)name delegate:(id)delegate;

 

@end

 

/*

 注意:在接收通知的过程中,必须先移除通知后再接收通知,否则会造成多次接收通知。

 */

 

 

 

.m文件

 

//

//  NotificationManager.m

//  DemoNotification

//

//  Created by zhangshaoyu on 14-7-11.

//  Copyright (c) 2014年 zhangshaoyu. All rights reserved.

//

 

#import "NotificationManager.h"

 

// 定义宏

#define NotificationShareManager [NSNotificationCenter defaultCenter]

 

@implementation NotificationManager

 

// 发送通知

+ (void)notificationSendWithName:(NSString *)name userInfo:(id)infoObj

{

    // 发出通知的方法

    // 方法1

//    [NotificationShareManager postNotificationName:name object:nil];

    

    // 方法2

//    NSNotification *notification = [NSNotification notificationWithName:name object:nil];

//    [NotificationShareManager postNotification:notification];

    

    // 方法3

    [NotificationShareManager postNotificationName:name

                                            object:nil

                                          userInfo:infoObj];

}

 

// 接收通知

+ (void)notificationReceiveWithName:(NSString *)name delegate:(id)delegate selector:(SEL)selector

{

    // 接收通知前先移除通知,避免多次接收相同的通知

    [self notificationRemoveWithName:name delegate:delegate];

    

    [NotificationShareManager addObserver:delegate

                                 selector:selector

                                     name:name

                                   object:nil];

}

 

// 移除通知

+ (void)notificationRemoveWithName:(NSString *)name delegate:(id)delegate

{

    [NotificationShareManager removeObserver:delegate

                                        name:name

                                      object:nil];

}

 

 

 

步骤2 使用

 

// 发送通知

[NotificationManager notificationSendWithName:NameNotification userInfo:nil];

 

// 接收通知

[NotificationManager notificationReceiveWithName:NameNotification delegate:self selector:@selector(notificationAction:)];

 

// 响应通知

- (void)notificationAction:(NSNotification *)notification

{

    NSLog(@"receive notification");

}

 

http://download.csdn.net/detail/potato512/7621713

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

番薯大佬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值