gpt4 book ai didi

ios - NSNotificationCenter removeObserver 不工作

转载 作者:可可西里 更新时间:2023-11-01 05:00:56 28 4
gpt4 key购买 nike

-(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 观察者。

最佳答案

这是在 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)
}

关于ios - NSNotificationCenter removeObserver 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33388940/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com