通知中心(NSNotificationCenter)ide
通知(NSNotification)post
- (NSString *)name; // 通知的名称
- (id)object; // 通知发布者(是谁要发布通知)
- (NSDictionary *)userInfo; // 一些额外的信息(通知发布者传递给通知接收者的信息内容)
+ (instancetype)notificationWithName:(NSString *)aName object:(id)anObject;
+ (instancetype)notificationWithName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;
- (instancetype)initWithName:(NSString *)name object:(id)object userInfo:(NSDictionary *)userInfo;
发布通知动画
通知中心(NSNotificationCenter)提供了相应的方法来帮助发布通知spa
- (void)postNotification:(NSNotification *)notification; //发布一个notification通知,可在notification对象中设置通知的名称、通知发布者、额外信息等
- (void)postNotificationName:(NSString *)aName object:(id)anObject; //发布一个名称为aName的通知,anObject为这个通知的发布者
- (void)postNotificationName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo; //发布一个名称为aName的通知,anObject为这个通知的发布者,aUserInfo为额外信息
注册通知监听器代理
通知中心(NSNotificationCenter)提供了方法来注册一个监听通知的监听器(Observer)code
//observer:监听器,即谁要接收这个通知 //aSelector:收到通知后,回调监听器的这个方法,而且把通知对象当作参数传入 //aName:通知的名称。若是为nil,那么不管通知的名称是什么,监听器都能收到这个通知 //anObject:通知发布者。若是为anObject和aName都为nil,监听器都收到全部的通知 - (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;
//name:通知的名称 //obj:通知发布者 //block:收到对应的通知时,会回调这个block //queue:决定了block在哪一个操做队列中执行,若是传nil,默认在当前操做队列中同步执行 - (id)addObserverForName:(NSString *)name object:(id)obj queue:(NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *note))block;
取消注册通知监听器server
- (void)removeObserver:(id)observer;
- (void)removeObserver:(id)observer name:(NSString *)aName object:(id)anObject;
- (void)dealloc { //[super dealloc]; 非ARC中须要调用此句 [[NSNotificationCenter defaultCenter] removeObserver:self];
}
UIDevice通知对象
UIDeviceOrientationDidChangeNotification // 设备旋转 UIDeviceBatteryStateDidChangeNotification // 电池状态改变 UIDeviceBatteryLevelDidChangeNotification // 电池电量改变 UIDeviceProximityStateDidChangeNotification // 近距离传感器(好比设备贴近了使用者的脸部)
键盘通知blog
UIKeyboardWillShowNotification // 键盘即将显示 UIKeyboardDidShowNotification // 键盘显示完毕 UIKeyboardWillHideNotification // 键盘即将隐藏 UIKeyboardDidHideNotification // 键盘隐藏完毕 UIKeyboardWillChangeFrameNotification // 键盘的位置尺寸即将发生改变 UIKeyboardDidChangeFrameNotification // 键盘的位置尺寸改变完毕
UIKeyboardFrameBeginUserInfoKey // 键盘刚开始的frame UIKeyboardFrameEndUserInfoKey // 键盘最终的frame(动画执行完毕后) UIKeyboardAnimationDurationUserInfoKey // 键盘动画的时间 UIKeyboardAnimationCurveUserInfoKey // 键盘动画的执行节奏(快慢)
通知和代理的选择队列
利用通知和代理都能完成对象之间的通讯(好比A对象告诉D对象发生了什么事情, A对象传递数据给D对象)