使用 [NSNotificationCenter defaultCenter] 发送的通知无论是在主线程还是子线程中被注册,观察者注册的选择器方法都会在主线程中被执行。
执行顺序:main runloop -> 发送通知 -> 观察者选择器方法(按照观察者注册的顺序执行)-> 通知发送者方法中其它的操作 -> main runloop
-
通知(消息)的创建
+ (instancetype)notificationWithName:(NSString *)aName object:(nullable id)anObject;
+ (instancetype)notificationWithName:(NSString *)aName object:(nullable id)anObject
userInfo:(nullable NSDictionary *)aUserInfo;
public convenience init(name aName: String, object anObject: AnyObject?)
public init(name: String, object: AnyObject?, userInfo: [NSObject : AnyObject]?)
参数说明:
aName :通知名称
anObject :传递给观察者的任意对象,通知发布者(是谁要发布通知)
aUserInfo:传递的消息内容,自定义字典,可以传递更多附加信息,一些额外的信息(通知发布者传递给通知接收者的信息内容)
-
Objective-C
// 不带消息内容
NSNotification *notification1 = [NSNotification notificationWithName:@"notification1"
object:self];
// 带消息内容
NSNotification *notification2 = [NSNotification notificationWithName:@"notification2"
object:self
userInfo:@{@"name":_name, @"age":_age}];
-
Swift
// 不带消息内容
let notification1 = NSNotification(name: "notification1",
object: self)
// 带消息内容
let notification2 = NSNotification(name: "notification2",
object: self,
userInfo: ["name":name, "age":age])
-
发送通知
- (void)postNotification:(NSNotification *)notification;
- (void)postNotificationName:(NSString *)aName object:(nullable id)anObject;
- (void)postNotificationName:(NSString *)aName object:(nullable id)anObject
userInfo:(nullable NSDictionary *)aUserInfo;
public func postNotification(notification: NSNotification)
public func postNotificationName(aName: String, object anObject: AnyObject?)
public func postNotificationName(aName: String, object anObject: AnyObject?,
userInfo aUserInfo: [NSObject : AnyObject]?)
参数说明:
notification:发送的通知(消息)
aName :通知名称
anObject :传递给观察者的任意对象,通知发布者
aUserInfo :传递的消息内容,自定义字典,可以传递更多附加信息
-
Objective-C
// 发送创建好的消息
[[NSNotificationCenter defaultCenter] postNotification:notification1];
// 直接发送消息,不带消息内容
[[NSNotificationCenter defaultCenter] postNotificationName:@"notification3"
object:self];
// 直接发送消息,带消息内容
[[NSNotificationCenter defaultCenter] postNotificationName:@"notification4"
object:self
userInfo:@{@"name":_name, @"age":_age}];
-
Swift
// 发送创建好的消息
NSNotificationCenter.defaultCenter().postNotification(notification1)
// 直接发送消息,不带消息内容
NSNotificationCenter.defaultCenter().postNotificationName("notification3",
object: self)
// 直接发送消息,带消息内容
NSNotificationCenter.defaultCenter().postNotificationName("notification4",
object: self,
userInfo: ["name":name, "age":age])
-
注册通知(观察者)
- (void)addObserver:(id)observer
selector:(SEL)aSelector
name:(nullable NSString *)aName
object:(nullable id)anObject;
public func addObserver(observer: AnyObject,
selector aSelector: Selector,
name aName: String?,
object anObject: AnyObject?)
参数说明:
observer :观察者,即谁要接收这个通知;
aSelector:收到通知后调用何种方法,即回调函数,并且把通知对象当做参数传入;
aName :通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。
为 nil 时,表示注册所有通知,那么无论通知的名称是什么,监听器都能收到这个通知;
anObject :通知发送者,为 nil 时,表示监听所有发送者的通知。如果 anObject 和 aName 都为 nil,监听器都收到所有的通知。
- (id)addObserverForName:(NSString *)name
object:(id)obj
queue:(NSOperationQueue *)queue
usingBlock:(void (^)(NSNotification *note))block;
参数说明:
name :通知的名称
obj :通知发布者
queue:决定了 block 在哪个操作队列中执行,如果传 nil,默认在当前操作队列中同步执行
block:收到对应的通知时,会回调这个 block
-
Objective-C
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(notification1Sel)
name:@"notification1"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(notification2Sel:)
name:@"notification2"
object:nil];
// 通知触发方法,通知无内容
- (void)notification1Sel {
}
// 通知触发方法,通知有内容
- (void)notification2Sel:(NSNotification *)notification {
// 接收用户消息内容
NSDictionary *userInfo = notification.userInfo;
}
-
Swift
NSNotificationCenter.defaultCenter().addObserver(self,
selector: #selector(ViewController.notification1Sel),
name: "notification1",
object: nil)
NSNotificationCenter.defaultCenter().addObserver(self,
selector: #selector(ViewController.notification2Sel(_:)),
name: "notification2",
object: nil)
// 通知触发方法,通知无内容
func notification1Sel() {
}
// 通知触发方法,通知有内容
func notification2Sel(notification:NSNotification) {
// 接收用户消息内容
let userInfo = notification.userInfo
}
-
移除通知(观察者)
- (void)removeObserver:(id)observer;
- (void)removeObserver:(id)observer name:(nullable NSString *)aName object:(nullable id)anObject;
public func removeObserver(observer: AnyObject)
public func removeObserver(observer: AnyObject, name aName: String?, object anObject: AnyObject?)
参数说明:
observer:观察者,即在什么地方接收通知;
aName :通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。
anObject:通知发送者,为 nil 时,表示移除满足条件的所有发送者的通知。
-
Objective-C
// 移除此观察者的所有通知
[[NSNotificationCenter defaultCenter] removeObserver:self];
// 移除指定名字的通知
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"notification1" object:nil];
-
Swift
// 移除此观察者的所有通知
NSNotificationCenter.defaultCenter().removeObserver(self)
// 移除指定名字的通知
NSNotificationCenter.defaultCenter().removeObserver(self, name:"notification1", object:nil)