NSNotification】的更多相关文章

我们在开发程序的时候,程序内不同对象间的通信是不可避免的,iOS中主要有以下这些通信方式: iOS中的通信方式 图中按照耦合度的强弱和通信的形式(一对一还是一对多)进行了划分,这篇文章我们主要说一下Notifications. 通知机制想必大家都很熟悉,平常的开发中或多或少的应该都用过.它是Cocoa中一个非常重要的机制,能把一个事件发送给多个监听该事件的对象,而消息的发送者不需要知道消息接收者的任何信息,消息的接受者也只是向通知中心(NSNotificationCenter)注册监听自己感兴趣…
一. iOS 中KVC.KVO.NSNotification.delegate 在实际的编程中运用的非常多,掌握好他们的运行原理和使用场合对于我们程序的开发将会带来事办工倍的效果:   二. KVC  key-Value coding,键值编码,级通过制定的key我们能查找到对应的value,也能给对应的key附上value值.这一点类似于我们的字典查找.比如我们需要查找哪一页的内容,只需要将字典的唯一的页码(也就是key)找到,就能够知道这一页的内容,根据这个字典这一特性,程序猿们发明了Dic…
一. 通知(NSNotification)的发送(Post)和注册(add)都必须借助于通知中心(NSNotificationCenter) 发送通知: NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; [center postNotificationName:@"通知名字" object:self userInfo:@{@"key":@"object"}];…
1  文本输入,键盘显示时,view向上,键盘隐藏时,view向下 1.1 注册键盘显示,关闭通知,并实现主界面上下变动 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserv…
1.KVC,即是指 NSKeyValueCoding,一个非正式的Protocol,提供一种机制来间接访问对象的属性.而不是通过调用Setter.Getter方法访问.KVO 就是基于 KVC 实现的关键技术之一. Demo: @interface myPerson : NSObject { NSString*_name; int      _age; int      _height; int      _weight; } @end @interface testViewController…
一.Delegate Delegate本质是一种程序设计模型,iOS中使用Delegate主要用于两个页面之间的数据传递.iphone中常用@protocol和delegate的机制来实现接口的功能.例如想在A的功能要在B中实现,可以在A中定义一个Protocol. protocol用法: @interface ClassA :ClassB<protocol1, protocol2> 1.首先声明一个UIView类: @interface myView  :UIView{  } @end: 2…
如果在一个类中想要执行另一个类中的方法可以使用通知 1.创建一个通知对象:使用notificationWithName:object: 或者 notificationWithName:object:userInfo: NSNotification* notification = [NSNotification notificationWithName:kImageNotificationLoadFailed(connection.imageURL)                       …
ios中实现callback可以通过两种方法,委托和NSNotification 委托的话是一对一的关系,例如一个UIViewController里有一个tableView, 将该viewController设置为tableView的委托,tableView执行的时候调用委托的函数,同时可能需要得到反馈,比如tableView通过调用delegate中rowsOfSections函数来得到该tableView的行数,调用委托函数的时候也可能不需要反馈,例如UIApplication的在AppDe…
本文主要借探讨NSNotificationName的最佳写法的机会,学习下extern, static, const, #define 和常量指针与指针常量等的特性和用法. 1.NSNotification标准使用方法 发送通知 [[NSNotificationCenter defaultCenter] postNotificationName:QLPosterDidPostNotification object:nil]; 接收通知 [[NSNotificationCenter default…
最近在github上看到了LRNotificationObserver这个项目,看了一下实现方式,作者通过ARC机制实例化注册对象子类与关联对象的方法来管理注册对象的生命周期.从而省去了系统通知移除的过程,本篇介绍该项目实现过程. NSNotificationCenter 通知存在的问题 注册 [[NSNotificationCenter defaultCenter] addObserver:anObserver selector:@selector(handleNotification:) n…