S.J*_*S.J 4 objective-c nsnotificationcenter ios
-(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");
}
Run Code Online (Sandbox Code Playgroud)
我SShot删除了观察者后,我正在登录控制台.
有没有其他方法可以删除UIApplicationUserDidTakeScreenshotNotification观察者.
Har*_*ris 15
以下是如何在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)
}
Run Code Online (Sandbox Code Playgroud)
Leo*_*rdo 11
来自Apple Doc:
要取消注册观察,请将此方法返回的对象传递给removeObserver:.你必须调用removeObserver:或removeObserver:名称:对象:对象:队列:usingBlock:由addObserverForName指定的任何对象之前被释放.
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center removeObserver:self.localeChangeObserver];
Run Code Online (Sandbox Code Playgroud)
你试图删除worng观察者,self这里不是观察者,观察者是add方法返回的对象
| 归档时间: |
|
| 查看次数: |
2865 次 |
| 最近记录: |