iOS NSNotification 通知机制

本文详细介绍了如何使用NSNotificationCenter对象在iOS应用中发布、监听和移除通知,包括通知对象的创建、参数设置及方法实现。通过实例展示了如何监听键盘事件、UITextField内容改变、手机联网状态变化及程序进入后台通知,以及如何在对象的dealloc方法中移除通知监听。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

NSNotificationCenter

发布通知,监听通知,移除通知,它们都是使用NSNotificationCenter对象来实现;特别需要注意的是监听通知以后一定要在监听通知的对象的dealloc方法中移除监听;

NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter]; // 获取NSNotificationCenter对象

NSNotification

通知对象,包含了通知的发出者,通知的名称,通知的信息内容等;

NSSender *sender; // sender
-(void) m1:(NSNotification *)notiInfo
{
    notiInfo.name;  // 监听到的通知名称;
    notiInfo.object;  // 监听到的通知的发布者;
    notiInfo.userInfo;  // 一个字典对象,该字典中包含了通知的具体内容;
}
NSListenr *listener;  // listener
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];

<1>. 使用listener监听一个通知

param1:要监听通知的对象
param2:用监听通知的对象的该方法来监听这个通知
param3:被监听的通知的名称
param4:发布通知的对象

监听通知

[notificationCenter addObserver:listener selector:@selector(m1:) name:@"noti_name" object:sender];

如果没有指定参数3(或者参数3为nil),但是指定了参数4,则表示凡是该sender对象发布的通知,listener的m1方法都将监听到;
如果指定了参数3(指定要监听某个通知),但是没有指定参数4(参数4为nil),则表示无论是哪个对象发布的与给定的通知名称相同的通知,都将被监听到;
如果参数3和参数4都为nil,则表示所有对象发布的所有通知,该方法都会监听到;

<2>. 用sender发布一个通知,需要通过NSNotificationCenter

param1:通知的名称
param2:通知的发布者
param3:通知的具体内容

[notificationCenter postNotificationName:@"noti_name" object:sender userInfo:@{@"key":@"value"}];

发布通知的其它方法

postNotification:(NSNotification *)notification;  // 直接发送一个NSNotification对象;
postNotificationName:(NSString *)name object:(id)object; // 发布一个名称为name的通知,object为通知的发布者;

<3>. 移除通知(通常需要重写监听者的dealloc方法)

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

实例

监听键盘的弹出,关闭事件

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(keyboardWillChangeFrame:) name:
UIKeyboardWillChangeFrameNotification object:nil];

-(void)keyboardWillChangeFrame:(NSNotification *)notiInfo
{
    CGRect endRect = [notiInfo.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    // 获取键盘的y值;
    CGFloat keyboardY = endRect.origin.y;
    CGFloat tranform = keyboardY - self.view.frame.size.height;
    // 滚动view
    self.view.transform = CGAffineTransformMakeTranslation(0,transform);
}

通过通知监听UITextField内容改变

[center addObserver:self  selector:@selector(textChange) name:UITextFieldTextDidChangeNotification  object:_mTextField];

移除程序进入后台通知

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil]

监听手机联网状态改变事件

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStateChange) name:kReachabilityChangedNotification object:nil];
self.conn = [Reachability reachabilityForInternetConnection];
[self.conn startNotifier];
// 重写dealloc方法
[self.conn stopNotifier];
[[NSNotificationCenter defaultCenter] removeObserver:self];

检测wifi状态

Reachability *wifi = [Reachability reachabilityForLoacalWifi];
[wifi currentReachabilityStatus] != NotReachable  // 有连接上的wifi;
Reqchability *conn = [Reachability reachabilityForInternetConnection];  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值