NSNotificationCenter removeObserver无法正常工作

时间:2015-10-28 10:44:34

标签: ios objective-c nsnotificationcenter

-(void)viewDidAppear:(BOOL)animated {
            NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
                [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationUserDidTakeScreenshotNotification object:nil queue:mainQueue usingBlock:^(NSNotification *note) {
                    NSLog(@"SShot");
            }];
        }

- (void)viewWillDisappear:(BOOL)animated{
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationUserDidTakeScreenshotNotification object:nil];
    NSLog(@"VWD");
        }

 -(void)viewDidDisappear:(BOOL)animated {
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationUserDidTakeScreenshotNotification object:nil];
        NSLog(@"VDD");
    }

即使我删除了观察者,我也会在SShot登录控制台。

是否有其他方法可以移除UIApplicationUserDidTakeScreenshotNotification观察者。

4 个答案:

答案 0 :(得分:13)

Here is how to do it in Swift 4...

    private var observer: NSObjectProtocol!

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        observer = NotificationCenter.default.addObserver(forName: NSNotification.Name("SomeNotification"), object: nil, queue: nil) { notification in
            //do something
        }
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        NotificationCenter.default.removeObserver(observer)
    }

答案 1 :(得分:9)

来自Apple Doc

  

要取消注册观察,您传递返回的对象   removeObserver的方法:您必须调用removeObserver:或   removeObserver:name:object:在指定的任何对象之前   addObserverForName:object:queue:usingBlock:is deallocated。

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center removeObserver:self.localeChangeObserver];

您尝试删除了worng观察者,self不是此处的观察者,观察者是add方法返回的对象

答案 2 :(得分:0)

尝试使用此代码

添加观察者

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDidTakeScreenshot) name:UIApplicationUserDidTakeScreenshotNotification object:nil];
}

- (void)userDidTakeScreenshot {
    // Screenshot taken, act accordingly.
}

删除特定观察者

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationUserDidTakeScreenshotNotification object:nil];
}

删除所有观察者

- (void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }

让我知道它是否适合你!!!!

答案 3 :(得分:0)

哈里斯(Harris)代码是正确的,除了一个小细节,对于Swift 4来说,现在是

私有var观察者:可以!而不是私有var观察者:NSObjectProtocol! 所以代码应该是:

private var observer: Any!

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    observer = NotificationCenter.default.addObserver(forName: NSNotification.Name("SomeNotification"), object: nil, queue: nil) { notification in
        //do something
    }
}

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)
    NotificationCenter.default.removeObserver(observer)
}