问题
Hi I am developing Iphone application in which am registering one notification observer for UIApplicationWillEnterForegroundNotification. Now I want to remove that one from another view controller. My code looks like
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(applicationBecomeActive)
name:UIApplicationWillEnterForegroundNotification
object:nil];
And I am creating one method for removing observer:
-(void) removeObserver
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
and I am calling this method from other view controller but its not working. I think I have to store observer. But I don't know how to do this. Need help. Thank you.
回答1:
The second view controller needs a reference to the instance of the first view controller. Let's assume it is hold in a property:
@property (nonatomic, strong) FirstViewControler *firstViewController;
Then your code to remove the fist view controller as an observer would look like this:
- (void)removeObserver
{
[[NSNotificationCenter defaultCenter] removeObserver:self.firstViewController];
}
The missing part is: You have to set the property somewhere. Where to do that depends on your code.
来源:https://stackoverflow.com/questions/23998145/remove-notification-observer-from-another-view-controller