[英]How to remove observer
我有一个启用了ARC的项目
在viewDidLoad中添加的观察者很少
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getSipNotification:) name:@"getSipNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(syncExtensionData:) name:@"syncExtensionData" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showLocalNotification:) name:@"showLocalNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(outgoingCall:) name:@"outgoingCall" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playRingtone) name:@"playRingtone" object:nil];
我想删除所有观察者,所以我在viewDidUnload中添加了以下行
[[NSNotificationCenter defaultCenter] removeObserver:self];
现在我的问题是,这会删除所有观察者吗?
如果没有,怎么办?
UPDATE
如果我想删除一个观察者,该怎么办?
你能帮我吗。
是的,它将删除所有观察者。
[[NSNotificationCenter defaultCenter] removeObserver:self];
您可以像这样删除特定的观察者...
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"syncExtensionData" object:nil];
在我的应用程序中,我使用了以下通知:
对于特定的观察者,请删除此方式:
-(void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceRotatedFeedBackView:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
-(void)deviceRotatedFeedBackView:(NSNotification*)notification
{
//right whetever you want
}
- (void)viewWillDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}
可能会对您有帮助。
是的,它将删除您班上的所有观察员。
您可以使用以下命令删除单个观察者:
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"getSipNotification" object:nil];
删除单个观察者。
在iOS6和更高版本中不建议使用viewDidUnload,因此您的观察者永远不会从iOS6和更高版本的通知中心中删除。 要删除单个观察者,请尝试
-(void)removeObserver:(id)notification观察者名称:(NSString *)notificationName对象:(id)notificationSender
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.