通知的使用
[[NSNotificationCenter defaultCenter]addObserver:selfselector:@selector
(noticeAction:) name:@"name" object:nil];
注册观察者
NSNotification* notice = [NSNotification notificationWithName:@"name" object:nil userInfo:params];
[[NSNotificationCenter defaultCenter]postNotification:notice];
创建一个通知并发送
观察者对象的注册一定要比通知的发送提前,否则的话会接收不到通知
通知和delegate的基本区别
通知是同步还是异步的?
通知的移除
异步通知
[[NSNotificationQueue defaultQueue]enqueueNotification:notice postingStyle:NSPostASAP];
----------------------------------------------------------------------------------typedef NS_ENUM(NSUInteger, NSPostingStyle) {
//空闲发送通知 当运行循环处于等待或空闲状态时,发送通知,对于不重要的通知可以使用。
NSPostWhenIdle = 1,
//尽快发送通知 当前运行循环迭代完成时,通知将会被发送,有点类似没有延迟的定时器。
NSPostASAP = 2,
//和postNotification一样是同步通知
NSPostNow = 3
};
三种枚举类型代表三种发送方式(异步/同步都可)
同样的,异步发送通知的话还可以使用开启一个新的线程的方式
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[[NSNotificationCenter defaultCenter]postNotification:notice];
});
通知的合并(待完善)
[[NSNotificationQueue defaultQueue] enqueueNotification:notice postingStyle:NSPostASAP coalesceMask:NSNotificationNoCoalescing forModes:nil];
-----------------------------------------------------------------------------------
typedef NS_OPTIONS(NSUInteger, NSNotificationCoalescing) {
// 不合成
NSNotificationNoCoalescing = 0,
// 根据NSNotification的name字段进行合成
NSNotificationCoalescingOnName = 1,
// 根据NSNotification的object字段进行合成
NSNotificationCoalescingOnSender = 2
};
NSNotificationQueue除了有异步通知的能力之外,也能对当前队列的通知根据NSNotificationCoalescing类型进行合并,需要配合不同的NSRunLoopMode来进行
通过合并通知我们可以用来保证相同的通知只被发送一次
NSNotificationCenter实现原理
NSNotificationCenter使用block方式添加的观察者
- (id<NSObject>)addObserverForName:(NSString *)name
object:(id)obj
queue:(NSOperationQueue *)queue
usingBlock:(void (^)(NSNotification *note))block
addObserver使用一个现存的对象作为观察者(一般为self),而使用block方法会创建一个匿名的(id
如果queue
为nil,则消息是默认在post
线程中同步处理,即通知的post
与转发是在同一线程中,不为nil的话就会在我们指定的队列中执行
如果一个给定的通知触发了多个观察者的block
操作,则这些操作会在各自的Operation Queue
中被并发执行。所以我们不能去假设操作的执行会按照添加观察者的顺序来执行
这个方法会返回一个匿名的观察者对象,我们需要手动释放这个对象。
属性传值
代理传值
在委托方定义协议,协议中声明@required/@optional方法,声明一个代理属性,使用weak修饰符
在代理方遵守协议,实现协议中的方法
初始化委托对象,将委托对象的代理设置为self(代理对象自身)
查看代理方是否实现了需要执行的选择子(SEL),如果实现了便开始执行
if ([self.delegate respondsToSelector:@selector(codeIOS)]) {
// 让代理方执行协议中的方法
[self.delegate SEL];
}
代理传值一般用于逆向传值,如:A控制器跳转到B控制器,B控制器为委托对象,A控制器为代理对象,B对象的代理为A对象,那么在B对象中可以传值给A对象,并在A对象中执行相应的方法
单例模式传值
使用block代码块进行传值
可以用于数据在多个对象间传递,网络请求的回调等
//在对象B中block作为方法参数
- (void)getNextPage:(void (^)(BOOL))completeBlock{
//block可以嵌套,第一层block可以在最后的block体中进行回调
[self getPage:self.page complete:^(NSMutableArray *result) {
if ([result count]) {
self.page++;
[self.musicList addObjectsFromArray:result];
if (completeBlock) {
//需要回调时,执行block
completeBlock(true);
}
}else{
if (completeBlock) {
completeBlock(false);
}
}
}];
}
------------------------------------------------------------------------------
//在对象A中isSucceed为回调回来的值
[strongSelf.musicModel getNextPage:^(BOOL isSucceed) {
if (isSucceed) {
[strongSelf.waterFallCollectionView reloadData];
}
}];
通知传值
NSNotification,NSNotificationCenter的使用、iOS中五种对象间传值的方式
原文:https://www.cnblogs.com/chenprice/p/12852735.html