1、通知中心,通知中心有两个方法,一个是发布通知,一个是订阅通知,必须先订阅通知, 再发布通知
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
2、订阅通知
MYPerson *zt = [[MYPerson alloc] init];
zt.name = @"MY";
MYPerson *ls = [[MYPerson alloc] init]; ls.name = @"LS"; //必须先有订阅者
//observer 通知的订阅者
//aSelector 接收到通知后做的事情
//aName 要接收的通知的名称,如果为nil 则接收所有通知
//anObject 发布者,接收谁发布的通知,如果为nil接收 所有人发布的通知
[center addObserver:zt selector: @selector(haoxiaoxi:) name: @"jinriguanzhu" object:nil];
3、发布通知
//先有订阅者,再发布通知
//发布通知 sanyuan 通知的发布者
[center postNotificationName:@"jinriguanzhu" object:guangzhou userInfo: @{@"xiaoxi":@"好消息"}];
[center postNotificationName:@"mainiumaile" object:shenzhen userInfo:
@{@"xiaoxi":@"坏消息"}];
4、在通知的接收者中处理通知的消息
//noti.object 通知的发布者
//noti.userInfo 发送者给接受者发送的信息 //noti.name 通知的名称
- (void)haoxiaoxi:(NSNotification *)noti {
//noti.name;
//收到的通知的名称
Company *com = noti.object; //通知的发布者
NSLog(@"%@",com.name);
//noti.userInfo; //发布通知的时候发送的额外信息 userInfo:
NSLog(@"%@",noti.userInfo[@"xiaoxi"]); }
5、当订阅者销毁,也要取消订阅通知,否则可能会出现野指针错误;
- (void)dealloc {
//在arc中不能,也不用调用[super dealloc];
//取消订阅
//在监听者的dealloc方法中,必须取消监听,否则,当通知再次出现,通知中心 任然回向该监听者发送消息
//因为对象已经释放,所以可能会导致崩溃
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
6、通知和代理的区别
1>相同点 代理和通知都能完成对象之间的通信(A对象告诉B对象发生了什么,A对象传 递数 据给B对象)
2>不同点
代理:1对1(1个对象,只能告诉另一个对象发生了什么)
通知:多对多(1个对象可以通知多个对象,1个对象可以订阅多个对象发布的通知)
7、键盘的通知事件
// 监听键盘的弹出和隐藏
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];;
//键盘的通知
//UIKeyboardWillShowNotification(键盘即将显示)
//UIKeyboardDidShowNotification(键盘已经显示)
//UIKeyboardWillHideNotification(键盘即将隐藏)
//UIKeyboardDidHideNotification(键盘已经隐藏)
//UIKeyboardWillChangeFrameNotification(键盘的位置尺寸即将发生改变)
//UIKeyboardDidChangeFrameNotification(键盘的位置尺寸已经发生改变)
//用来监听键盘的弹出/隐藏
-(void)keyboardWillChange:(NSNotification *)note{
/* 弹出时的信息
{name = UIKeyboardWillChangeFrameNotification;
userInfo = {
UIKeyboardAnimationCurveUserInfoKey = 7; // 动画执行节奏:先慢后快还是先快后慢
UIKeyboardAnimationDurationUserInfoKey = "0.25"; // 弹出所需要的时间(动画的执行时间)
UIKeyboardFrameBeginUserInfoKey = "NSRect: { {0, 451}, {375, 216}}"; // 键盘弹出前的位置和尺寸
UIKeyboardFrameEndUserInfoKey = "NSRect: { {0, 415}, {375, 252}}"; // 键盘弹出完毕后的位置和尺寸
}}*/
/* 隐藏时的信息
{name = UIKeyboardWillChangeFrameNotification;
userInfo = {
UIKeyboardAnimationCurveUserInfoKey = 7;
UIKeyboardAnimationDurationUserInfoKey = "0.25";
UIKeyboardBoundsUserInfoKey = "NSRect: { {0, 0}, {375, 252}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 541}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 793}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: { {0, 415}, {375, 252}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: { {0, 667}, {375, 252}}";
}}
*/
利用键盘弹出和隐藏时的key值获取位置、大小等
// 获得通知信息
NSDictionary *userInfo = note.userInfo;
// 获得键盘弹出后或隐藏后的frame
CGRect keyboardFrame =[userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
// 获得键盘的y值
CGFloat keyboardY = keyboardFrame.origin.y;
// 获得屏幕的高度
CGFloat screenH =[UIScreen mainScreen].bounds.size.height;
//
// 获得键盘执行动画的时间
CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
// 让执行操作能同步进行,必须设定options:7 << 16
[UIView animateWithDuration:duration delay:0.0 options:7 << 16 animations:^{
self.view.transform = CGAffineTransformMakeTranslation(0, keyboardY - screenH);
} completion:nil];
}