IOS 之 通知NSNotificationCenter

    通知,就是说A观察B的情况.如果B有所改动,就通知A让A知道.这是一种松耦合的通信方式.

有两种方法:

1. NSNotificationCenter通知中心

例子如下:

//构建自定义通知事件和发送
//通知的事件名称, 和 参数
NSNotification *notification = [NSNotification notificationWithName:@"data changes" object:self];
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter postNotification:notification];

//订阅这个通知后.上边发送的消息,这里才能收到
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
//update是收到通知后执行的方法, subject是上边传送过来的参数
[notificationCenter addObserver:self selector:@selector(update:) name:@"data changes" object:subject];

2. NSKeyValueObserving 键值对的方式

手动调用通知

在观察者的类里加上如下代码

//把自身作为观察者加到scribble里
//注册@"mark" 为键 scribble为值
[scribble addObserver:self forKeyPath:@"mark" 
						   opations:NSKeyValueObservingOptionInitial | 
									NSKeyValueObservingOptionNew
							context:nil];
收到通知后执行的方法

//在观察者的代码里加上处理收到通知后的方法
- (void) observeValueForKeyPath:(NSString *)keyPath 
					   ofObject:(id)object 
					     change:(NSDictionary *)change
						context:(void *)context
{
	//如果是来自注册mark的通知就执行并且判断发送通知的来源是否是Scribble类
	if( [object is KindOfClass:[Scribble class]] && [keyPath isEqualoString:@"mark"] )
	{
		//NSKeyValueChangeNewKey是用来获取Scribble里改动的值
		id mark = [change objectForKey:NSKeyValueChangeNewKey];
		//..一些处理
	}
}


在被观察者的类里加上

//在scribble类里有变动的地方执行以通知观察者
//这里执行完didChangeValueForKey后会发送一条通知执行以下代码
[self willChangeValueForKey:@"mark"];
....//一些改动
[self didChangeValueForKey:@"mark"];  



至此,通知差不多就是这,慢慢练练就是熟悉了!

iOS 中,可以使用通知NSNotification)机制来实现组件间的解耦和消息传递。通知是一种广播机制,一个组件可以发布(post)一个通知,其他组件可以订阅(observe)这个通知,并在接收到通知时执行相应的操作。下面是一个通知的实例说明。 假设我们有一个应用程序,在其中有两个视图控制器 AViewController 和 BViewController。AViewController 中有一个按钮,当用户点击按钮时,我们希望 BViewController 接收到一个通知,并在接收到通知时更新界面。 首先,在 BViewController 中,我们需要注册一个通知观察者,代码如下: ``` - (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"MyNotification" object:nil]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)handleNotification:(NSNotification *)notification { // 处理通知 NSLog(@"Received notification: %@", notification); } ``` 在这里,我们使用 defaultCenter 对象注册了一个观察者,指定了通知的名称为 MyNotification。当通知被发布时,handleNotification 方法会被调用,并且可以在方法中处理通知。 接下来,在 AViewController 中,我们需要发布一个通知,代码如下: ``` - (IBAction)postNotification:(id)sender { [[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" object:nil userInfo:@{@"key": @"value"}]; } ``` 在这里,我们使用 defaultCenter 对象发布了一个通知,指定了通知的名称为 MyNotification,没有指定通知的发送者,同时可以携带一些额外的信息,例如 userInfo 字典中的键值对。 当用户点击 AViewController 中的按钮时,就会触发 postNotification 方法,同时 BViewController 中的 handleNotification 方法也会被调用,并且可以在其中处理通知。 总之,通知机制是一种非常方便的组件间通信方式,可以实现解耦和消息传递。在使用通知时,需要注意及时注册和注销观察者,并且指定通知的名称和携带的信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值