【发布时间】:2015-09-17 16:38:23
【问题描述】:
在 IOS 9 之前,我的 ViewController 类包括 NSNotificationCenter 观察者,并且正确调用了 @selector() 方法(代码如下所示)。 但是从 iOS 9 开始,这些方法不再被调用。有没有人有同样的问题?我需要知道是否有解决方案,或者我需要从 AppDelegate 类对我的 viewController 进行委托调用。先感谢您。
请注意,我在 viewWillAppear 中有那些,在 viewWillDisappear 中有 removeObserver。
我查看了苹果文档https://developer.apple.com/library/prerelease/mac/releasenotes/Foundation/RN-Foundation/index.html#//apple_ref/doc/uid/TP30000742 它指出我们不再需要删除弱链接通知的观察者,所以我从 viewWillDisappear 中删除了“removeObserver”。
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(applicationDidEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationEnteringForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationBecameActive) name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationResignedActive) name:UIApplicationWillResignActiveNotification object:nil];
【问题讨论】:
-
有些人报告说延迟可能会有所帮助。可能是这样的: dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(applicationDidEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil]; });
标签: ios objective-c iphone ios9 xcode7