[[NSNotificationCenter defaultCenter] addObserver:selector:@selector(mymethod)name:object:];不再在IOS 9中调用

时间:2015-09-17 16:38:24

标签: ios objective-c iphone ios9 xcode7

在IOS 9之前,我的ViewController类包含NSNotificationCenter观察者,并且@selector()方法被正确调用(下面显示的代码)。 但是,从iOS 9开始,这些方法不再被调用。有没有人有同样的问题?我需要知道是否有解决方案,或者我需要从AppDelegate类调用Delegate到我的viewController。先感谢您。

请注意,我在viewWillDisappear中的viewWillAppear和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];

1 个答案:

答案 0 :(得分:3)

有些人报告延迟可能有所帮助。这样的事可能是: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]; });