您的位置:首页 > 其它

通知

2015-09-01 00:40 232 查看

通知

什么时候使用通知:当两个对象没有联系的时候,使用通知。让两个没有联系的对象产生关系。

发出通知 (postNotification)

新建一个通知(NSNotification)对象

一个完整的通知一般包含三个属性:

(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 发布已经创建好的通知

- (void)postNotification:(NSNotification *)notification;


//新建通知note
NSNotification *note = [NSNotification notificationWithName:@"快去干活" object:self userInfo:@{@"name":@"哈哈哈"}];
//发布通知
[[NSNotificationCenter defaultCenter] postNotification:note];


或者不创建,直接发布通知也可以:(把前面两个步骤合起来)

//发布一个名称为aName的通知,anObject为这个通知的发布者

- (void)postNotificationName:(NSString *)aName object:(id)anObject;

//发布一个名称为aName的通知,anObject为这个通知的发布者,aUserInfo为额外信息

- (void)postNotificationName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;


[[NSNotificationCenter defaultCenter] postNotificationName:@"note" object:self];


接受通知:监听者 (addObserver)

注意:
必须是先有监听者,再发布通知。如果发布通知的时候没有监听者,通知不会被接收到。


方法一:

Observer;谁监听通知

selector:当监听到通知的时候就会调用selector方法

name:监听的通知名称 如果是nil,那么object发出的随便什么通知都监听

object:监听谁发送的通知 如果是nil:那么无论谁发出的通知都监听

[[NSNotificationCenter defaultCenter] addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;
- (void)selector:(NSNotification *)note{
//selector方法可以把通知作为参数带到这里,并且拿到通知的各个属性.(也可以不带参数)
//此处object是id类型,不能用点语法,必须转换一下
NSLog(@"%@%@%@",note.name,[note.object name],note.userInfo);
}


更高级的用法:监听键盘弹出可以不用发通知,因为系统会默认发,详见下面

方法二:

name:通知的名称

obj:通知发布者

block:收到对应的通知时,会回调这个block

queue:决定了block在哪个操作队列中执行,如果传nil,默认在当前操作队列中同步执行

- (id)addObserverForName:(NSString *)name object:(id)obj queue:(NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *note))block;


当对象销毁的时候,需要取消监听

通知中心不会保留(retain)监听器对象,在通知中心注册过的对象,必须在该对象释放前取消注册。否则,当相应的通知再次出现时,通知中心仍然会向该监听器发送消息。因为相应的监听器对象已经被释放了,所以可能会导致应用崩溃

//监听者是对象:
[[NSNotificationCenter defaultCenter] removeObserver:p1];
//监听者是控制器:

- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}


UIDevice

UIDevice是一个单例对象,代表着设备。可以通过[UIDevice currentDevice] 获得

查看版本号:systemVersion。根据不同版本号做不同适配

double version = [UIDevice currentDevice].systemVersion.doubleValue;
if (version >= 8.0) {
//ios8以上
} else if (version >= 7.0) {
//ios7以上
} else {

}


UIDevice通知

UIDevice对象会不间断地发布一些通知,下列是UIDevice对象所发布通知的名称常量:

UIDeviceOrientationDidChangeNotification // 设备旋转

UIDeviceBatteryStateDidChangeNotification // 电池状态改变

UIDeviceBatteryLevelDidChangeNotification // 电池电量改变

UIDeviceProximityStateDidChangeNotification // 近距离传感器(比如设备贴近了使用者的脸部)

可以添加监听者来监听这些通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryLevelChange) name:UIDeviceBatteryLevelDidChangeNotification object:nil];
- (void)batteryLevelChange
{
NSLog(@"电池电量不足");
}


键盘通知

我们经常需要在键盘弹出或者隐藏的时候做一些特定的操作,因此需要监听键盘的状态 l l

键盘状态改变的时候,系统会发出一些特定的通知

UIKeyboardWillShowNotification // 键盘即将显示

UIKeyboardDidShowNotification // 键盘显示完毕

UIKeyboardWillHideNotification // 键盘即将隐藏

UIKeyboardDidHideNotification // 键盘隐藏完毕

UIKeyboardWillChangeFrameNotification // 键盘的位置尺寸即将发生改变

UIKeyboardDidChangeFrameNotification // 键盘的位置尺寸改变完毕

系统发出键盘通知时,会附带一下跟键盘有关的额外信息(字典),字典常见的key如下: (可以用通知.userInfo拿到)

UIKeyboardFrameBeginUserInfoKey // 键盘刚开始的frame

UIKeyboardFrameEndUserInfoKey // 键盘最终的frame(动画执行完毕后)

UIKeyboardAnimationDurationUserInfoKey // 键盘动画的时间

UIKeyboardAnimationCurveUserInfoKey // 键盘动画的执行节奏(快慢)

具体演示屏幕下方的条随键盘高度而调整位置



// 添加监听者,其中UIKeyboardWillChangeFrameNotification这个通知是系统发的,不需要我们去发
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:Nil];
}
// 接受到通知后的回调方法,把通知作为参数传过来
- (void)keyboardWillChange:(NSNotification *)note{
// note.userInfo字典中的key拿到键盘形变的时间
double duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
[UIView animateWithDuration:duration animations:^{
    // note.userInfo字典中的key拿到键盘形变frame的高度
CGFloat keboardY = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y;
    // 让底部条也形变
self.bottomBar.transform = CGAffineTransformMakeTranslation(0, keboardY - K2SScreenH);
}];
}


UITableView弹出键盘适应高度

ios7之后,
只要把textField添加到需要弹出键盘的cell上面,就会自动做好键盘处理


//这个方法可以拿到当前tableView的cell
UITableViewCell *cell = [Self.tableView cellForRowAtIndexPath:indexPath];
UITextField *textField = [[UITextField alloc] init];
//成为第一响应者,就会弹出键盘了
[textField becomeFirstResponder];
[cell addSubview:textField];


当滚动的时候,关掉键盘

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self.view endEditing:YES];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: