NSNotificationCenter removeObserver无效

11
-(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个回答

21

以下是如何在Swift 4中完成它的步骤...

    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)
    }

20

来自苹果文档:

要取消观察,您需要将此方法返回的对象传递给removeObserver:。在任何由addObserverForName:object:queue:usingBlock:指定的对象被解除分配之前,都必须调用removeObserver:或removeObserver:name:object:

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

你试图移除错误的观察者,self 在这里不是观察者,观察者是由add方法返回的对象。


3
哈里斯的代码是正确的,只有一个小问题,对于Swift 4,现在应该是private var observer: Any!而不是private var observer: 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)
}

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];
    }

如果它对你有用,请告诉我!!!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接