参考:链接:https://www.jianshu.com/p/c22545a5bd0a
链接:https://www.jianshu.com/p/c22545a5bd0a
NSNotification和Delegate的联系和区别
众所周知,IOS中经常会使用到NSNotification和delegate来进行一些类之间的消息传递。言归正传,这两种有什么区别呢?
NSNotification就是IOS提供的一个消息中心,由一个全局的defaultNotification管理应用中的消息机制。通过公开的API可以看出,这里面使用了是一个观察者,通过注册addObserver和解除注册removeObserver来实现消息传递。苹果文档特别提出,在类析构的时候,要记得把removeObserver,不然就会引发崩溃,所以NSNotifcation的使用是没有retain+1的,NSNotification是一对多的。
至于Delegate,很简单,就是通过增加一个指针,然后把需要调用的函数通过delegate传递到其他类中,来得很直截了当。不需要通过广播的形式去实现,但是,delegate的形式只能是一对一,不能实现一对多。
在什么情况下使用Delegate和NSNotifiation呢?
从效率上看Delegate是一个很轻量级的,相对delegate,NSNotification却是一个很重量级的,效率上delegate明显要比Noticication高。一般情况我们会这样使用。
场景一:
A拥有B,然后B中的一些操作需要回调到A中,这时候就简单的通过delegate回调到A。因为B是A创建的,B可以很直接的把delegate赋值A。
场景二:
A和B是两个不相干的关系,A不知道B,B也不知道A,那么这时候如果通过delegate就没办法做到,会相对复杂。所以可以通过NSNotifcation去做一些消息传递。
所以使用delegate的情况是两者有直接的关系,至于一方知道另一方的存在。而NSNotifcation一般是大家不知道对方的存在,一般是使用跨模块的时候使用。在使用的时候,使用delegate可能需要多写一些delegate去实现,代码量比较多。NSNotication只要定义相关的NotificationName就可以很方便的沟通。两者各有所长。
监听系统自带的NSNotification
系统里定义了许多的 XxxNotification 名称,其实只要 Cmd+Shift+O 打开 Open Quickly,输入 NSNotification 或者 UINotification 可以看到许多以 Notification 结尾的变量定义,由变量名称也能理解在什么时候会激发什么事件,一般都是向 [NSNotificationCenter defaultCenter] 通知的。
注册系统监听事件:
//在NSNotificationCenter中注册键盘弹出事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardUpEvent:) name:UIKeyboardDidShowNotification object:nil];
//在NSNotificationCenter中注册键盘隐藏事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDownEvent:) name:UIKeyboardDidHideNotification object:nil];
//在NSNotificationCenter中注册程序从后台唤醒事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(becomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
函数借口说明
NSNotification类其比较重要的属性和方法如下:
//通知的名称,有时可能会使用一个方法来处理多个通知,可以根据名称区分
@property (readonly, copy) NSNotificationName name;
//通知的对象,常使用nil,如果设置了值注册的通知监听器的object需要与通知的object匹配,否则接收不到通知
@property (nullable, readonly, retain) id object;
//字典类型的用户信息,用户可将需要传递的数据放入该字典中
@property (nullable, readonly, copy) NSDictionary *userInfo;
//下面三个是NSNotification的构造函数,一般不需要手动构造
- (instancetype)initWithName:(NSNotificationName)name object:(nullable id)object userInfo:(nullable NSDictionary *)userInfo API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)) NS_DESIGNATED_INITIALIZER;
+ (instancetype)notificationWithName:(NSNotificationName)aName object:(nullable id)anObject;
+ (instancetype)notificationWithName:(NSNotificationName)aName object:(nullable id)anObject userInfo:(nullable NSDictionary *)aUserInfo;
NSNotification通知类本身很简单,需要着重理解的就是其三个属性,接下来看一下NSNotificationCenter通知中心,通知中心采用单例的模式,整个系统只有一个通知中心,通过如下代码获取:
[NSNotificationCenter defaultCenter]
再看一下通知中心的几个核心方法:
/*
注册通知监听器,只有这一个方法
observer为监听器
aSelector为接到收通知后的处理函数
aName为监听的通知的名称
object为接收通知的对象,需要与postNotification的object匹配,否则接收不到通知
*/
- (void)addObserver:(id)observer selector:(SEL)aSelector name:(nullable NSNotificationName)aName object:(nullable id)anObject;
/*
发送通知,需要手动构造一个NSNotification对象
*/
- (void)postNotification:(NSNotification *)notification;
/*
发送通知
aName为注册的通知名称
anObject为接受通知的对象,通知不传参时可使用该方法
*/
- (void)postNotificationName:(NSNotificationName)aName object:(nullable id)anObject;
/*
发送通知
aName为注册的通知名称
anObject为接受通知的对象
aUserInfo为字典类型的数据,可以传递相关数据
*/
- (void)postNotificationName:(NSNotificationName)aName object:(nullable id)anObject userInfo:(nullable NSDictionary *)aUserInfo;
/*
删除通知的监听器
*/
- (void)removeObserver:(id)observer;
/*
删除通知的监听器
aName监听的通知的名称
anObject监听的通知的发送对象
*/
- (void)removeObserver:(id)observer name:(nullable NSNotificationName)aName object:(nullable id)anObject;
/*
以block的方式注册通知监听器
*/
- (id <NSObject>)addObserverForName:(nullable NSNotificationName)name object:(nullable id)obj queue:(nullable NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *note))block API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
接下来举一个栗子,和之前delegate的栗子相同,只不过这里使用通知来实现,依旧是两个页面,ViewController和NextViewController,在ViewController中有一个按钮和一个标签,点击按钮跳转到NextViewController视图中,NextViewController中包含一个输入框和一个按钮,用户在完成输入后点击按钮退出视图跳转回ViewController并在ViewController的标签中展示用户填写的数据,接下来看一下代码:
//ViewController部分代码
- (void)viewDidLoad
{
//注册通知的监听器,通知名称为inputTextValueChangedNotification,处理函数为inputTextValueChangedNotificationHandler:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(inputTextValueChangedNotificationHandler:) name:@"inputTextValueChangedNotification" object:nil];
}
//按钮点击事件处理器
- (void)buttonClicked
{
//按钮点击后创建NextViewController并展示
NextViewController *nvc = [[NextViewController alloc] init];
[self presentViewController:nvc animated:YES completion:nil];
}
//通知监听器处理函数
- (void)inputTextValueChangedNotificationHandler:(NSNotification*)notification
{
//从userInfo字典中获取数据展示到标签中
self.label.text = notification.userInfo[@"inputText"];
}
- (void)dealloc
{
//当ViewController销毁前删除通知监听器
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"inputTextValueChangedNotification" object:nil];
}
//NextViewController部分代码
//用户完成输入后点击按钮的事件处理器
- (void)completeButtonClickedHandler
{
//发送通知,并构造一个userInfo的字典数据类型,将用户输入文本保存
[[NSNotificationCenter defaultCenter] postNotificationName:@"inputTextValueChangedNotification" object:nil userInfo:@{@"inputText": self.textField.text}];
//退出视图
[self dismissViewControllerAnimated:YES completion:nil];
}
NSNotificationCenter的使用步骤
1、在需要监听某通知的地方注册通知监听器
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(inputTextValueChangedNotificationHandler:) name:@"inputTextValueChangedNotification" object:nil];
2、实现通知监听器的回调函数
- (void)inputTextValueChangedNotificationHandler:(NSNotification*)notification
{
self.label.text = notification.userInfo[@"inputText"];
}
3.在监听器对象销毁前删除通知监听器
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"inputTextValueChangedNotification" object:nil];
}
4、如有通知需要发送,使用NSNotificationCenter发送通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"inputTextValueChangedNotification" object:nil userInfo:@{@"inputText": self.textField.text}];
线程问题
上面的栗子很简单,但有一点是需要强调的,我们在NextViewController中发送的通知是在main线程中发送的,因此ViewController中的监听器回调函数也会在main线程中执行,因此我们在监听器回调函数中修改UI不会产生任何问题,但当通知是在其他线程中发送的,监听器回调函数很有可能就是在发送通知的那个线程中执行,我们知道UI的更新必须在主线程中执行,这个时候就需要注意,如果通知监听器回调函数有需要更新UI的代码,需要使用GCD放在主线程中执行,代码如下:
//NextViewController发送通知的代码修改为如下代码:
- (void)completeButtonClickedHandler
{
//使用GCD获取一个非主线程的线程用于发送通知
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"inputTextValueChangedNotification" object:nil userInfo:@{@"inputText": self.textField.text}];
});
[self dismissViewControllerAnimated:YES completion:nil];
}
//ViewController通知监听器的回调函数修改为如下代码:
- (void)inputTextValueChangedNotificationHandler:(NSNotification*)notification
{
//使用GCD获取主线程并更新UI
dispatch_async(dispatch_get_main_queue(), ^{
self.label.text = notification.userInfo[@"inputText"];
});
//如果不在主线程更新UI很有可能无法正确执行
//self.label.text = notification.userInfo[@"inputText"];
}
很多时候我们使用的是第三方框架发送的通知,或是系统提供的通知,我们无法预知这些通知是否是在主线程中发送的,为了安全起见最好在需要更新UI时使用GCD将更新的逻辑放入主线程执行。
系统提供了很多各式各样的通知,比如当我们要实现IM即时通讯类app的聊天页面输入框时就可以使用系统键盘发出的通知,相关通知有UIKeyboardWillShowNotification和UIKeyboardWillHideNotification,顾名思义一个是键盘即将展示,一个是键盘即将退出的通知,接下来给一个简单的实现:
#import "ViewController.h"
#define ScreenWidth [[UIScreen mainScreen] bounds].size.width
#define ScreenHeight [[UIScreen mainScreen] bounds].size.height
@interface ViewController ()
@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) UITextField *textField;
@end
@implementation ViewController
@synthesize containerView = _containerView;
@synthesize textField = _textField;
- (instancetype)init
{
if (self = [super init])
{
self.view.backgroundColor = [UIColor whiteColor];
//创建一个容器View可自定义相关UI
self.containerView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight - 60, ScreenWidth, 60)];
self.containerView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.containerView];
//用户输入的UITextField
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 10, ScreenWidth - 40, 40)];
self.textField.placeholder = @"input...";
self.textField.backgroundColor = [UIColor greenColor];
[self.containerView addSubview:self.textField];
[self.view addSubview:self.containerView];
//添加一个手势点击空白部分后收回键盘
UIGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView)];
[self.view setMultipleTouchEnabled:YES];
[self.view addGestureRecognizer:gesture];
}
return self;
}
- (void)viewDidLoad
{
//注册UIKeyboardWillShowNotification通知,监听键盘弹出事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//注册UIKeyboardWillHideNotification通知,监听键盘回收事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
//自定义手势响应处理器
- (void)tapView
{
//触发收回键盘事件
[self.textField resignFirstResponder];
}
//UIKeyboardWillShowNotification通知回调函数
- (void)keyboardWillShow:(NSNotification*)notification
{
//获取userInfo字典数据
NSDictionary *userInfo = [notification userInfo];
//根据UIKeyboardBoundsUserInfoKey键获取键盘高度
float keyboardHeight = [[userInfo objectForKey:@"UIKeyboardBoundsUserInfoKey"] CGRectValue].size.height;
//获取键盘弹出的动画时间
NSTimeInterval animationDuration;
[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
//自定义动画修改ContainerView的位置
[UIView animateWithDuration:animationDuration animations:^{
self.containerView.frame = CGRectMake(0, ScreenHeight - keyboardHeight - self.containerView.frame.size.height, self.containerView.frame.size.width, self.containerView.frame.size.height);
}];
}
//UIKeyboardWillHideNotification通知回调函数
- (void)keyboardWillHide:(NSNotification*)notification
{
//获取动画执行执行时间
NSValue *animationDurationValue = [[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
//自定义动画修改ContainerView的位置
[UIView animateWithDuration:animationDuration animations:^{
self.containerView.frame = CGRectMake(0, ScreenHeight - self.containerView.frame.size.height, self.containerView.frame.size.width, self.containerView.frame.size.height);
}];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
@end
如果当我们不是百分之百确认通知的发送队列是在主队列中时,我们最好加上如下代码从而对我们的UI进行处理。
if (strcmp(dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL), dispatch_queue_get_label(dispatch_get_main_queue())) == 0) {
//UI处理
} else {
dispatch_async(dispatch_get_main_queue(), ^{
//UI处理
});
}