iOS NSNotificationCenter通知中心传值
2020-05-14 本文已影响0人
CarrySniper
主要方法:
/// 添加通知中心观察者
/// @param observer 观察者
/// @param aSelector 选择器
/// @param aName 名称
/// @param anObject 对象
- (void)addObserver:(id)observer selector:(SEL)aSelector name:(nullable NSNotificationName)aName object:(nullable id)anObject;
/// 移除通知中心观察者
/// @param observer 观察者
/// @param aName 名称
/// @param anObject 对象
- (void)removeObserver:(id)observer name:(nullable NSNotificationName)aName object:(nullable id)anObject;
/// 投递通知
/// @param aName 名称
/// @param anObject 对象(可传)
/// @param aUserInfo 用户信息(传参字典集合)
- (void)postNotificationName:(NSNotificationName)aName object:(nullable id)anObject userInfo:(nullable NSDictionary *)aUserInfo
/// 定义静态常量
static NSString * const kNotification_NeedLogin = @"kNotification_NeedLogin";
使用方法:
1、在需要监听到地方(任何类),添加通知中心观察者,只要有触发,就会回调方法
observer:self(当前类)
selector:showAccountPage:
name:kNotification_NeedLogin的值
object:对象nil
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
/// 添加通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showAccountPage:) name:kNotification_NeedLogin object:nil];
}
2、在不需要的时候,一定要移除,不要重复添加。避免内存泄漏,应用崩溃。
observer:self(当前类)
name:kNotification_NeedLogin的值
object:对象nil
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
/// 移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self name:kNotification_NeedLogin object:nil];
}
3、发起投递通知,进行通知并传参数。
/// 定义参数集合
NSDictionary *userInfo = @{@"username":@"CarrySniper"};
/// 投递方式一:
[[NSNotificationCenter defaultCenter] postNotificationName:kNotification_OpenFace object:nil userInfo:userInfo];
/// 投递方式二:
NSNotification *notification = [NSNotification notificationWithName:kNotification_OpenFace object:nil userInfo:userInfo];
[[NSNotificationCenter defaultCenter] postNotification:notification];
4、实现通知响应方法,并接收参数。
/// 通知响应实现方法
/// @param notification 通知对象
- (void)showAccountPage:(NSNotification *)notification {
NSLog(@"整个集合%@", notification.userInfo);
NSLog(@"取参数 username = %@", [notification.userInfo objectForKey:@"username"]);
}
题外:通知并且添加Block回调
/// 发起通知
/// @param completionHandler 回调
+ (void)notificationShowAccountPage:(void(^)(bool successful))completionHandler {
/// 方式一:使用了object
[[NSNotificationCenter defaultCenter] postNotificationName:kNotification_NeedLogin object:completionHandler];
/// 方式二:使用了userInfo
[[NSNotificationCenter defaultCenter] postNotificationName:kNotification_NeedLogin object:nil userInfo:@{@"block": completionHandler}];
}
/// 通知响应实现方法
/// @param notification 通知对象
- (void)showAccountPage:(NSNotification *)notification {
/// 方法一:object对象的
void(^loginBlock)(bool loginSuccessful) = [notification object];
if (loginBlock) {
/// 登录成功
loginBlock(YES);// 调用
/// 登录失败
loginBlock(NO);// 调用
}
/// 方法二:userInfo对象的
NSDictionary *userInfo = [notification userInfo] ;
void(^loginBlock)(bool loginSuccessful) = [userInfo objectForKey:@"block"];
if (loginBlock) {
/// 登录成功
loginBlock(YES);// 调用
/// 登录失败
loginBlock(NO);// 调用
}
}
/// 调用的地方
[XXXXX notificationShowFaceAuthPage:^(bool successful) {
if (successful) {
/// 成功处理
} else {
/// 失败处理
}
}];