我有一个读取条形码的类,当我读取条形码时,我会向NSNotificationCenter发送一个通知,如下所示。
-(void)barcodeData:(NSString *)barcode type:(int)type {
barcodeValue = barcode;
[[NSNotificationCenter defaultCenter] postNotificationName:@"BarcodeRead" object:self];
}然后在几个视图控制器中,我添加观察者来获得条形码值,如下所示。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(BarcodeRead) name:@"BarcodeRead" object:nil];
-(void) BarcodeRead
{
//
}问题是,当通知被发送到通知中心时,在我添加观察者的所有视图中,他们都会获得通知并调用方法BarcodeRead,但我希望如果应用程序在视图控制器"A“中,只需要A获得通知,而不是所有的通知。
谢谢你的帮助
发布于 2011-11-06 22:04:16
我通常将注册/注销代码放入viewWillAppear / viewWillDisappear方法中,以确保只有在控制器处于活动状态时才会显示通知。
发布于 2011-11-06 21:43:13
然后,你应该让那些不应该收到通知的对象在它们离开屏幕后注销自己作为观察者(当然,当它们重新出现在屏幕上时重新注册)。
https://stackoverflow.com/questions/8027017
复制相似问题