在iOS 9中,以下用于检测通知的代码不会触发选择器方法。在之前的版本(例如8.4)中,它运行良好。有谁知道为什么?
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive)name:UIApplicationDidBecomeActiveNotification object:nil];
...
- (void)applicationDidBecomeActive
{
NSLog(@"1");
}
AppDelegate.m
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(@"2");
}
答案 0 :(得分:0)
我在AppDelegate中注册了UIApplicationDidBecomeActiveNotification:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(handleDidBecomeActive)
name: UIApplicationDidBecomeActiveNotification
object: nil];
...
}
选择器handleDidBecomeActive将在启动应用程序开始时直接调用。
-(void)handleDidBecomeActive
{
NSLog(@"AppDelegate: didBecomeActive");
}
这适用于iOS9。
我试图在一个将在稍后创建的类(连接到rootViewController)中获取UIApplicationDidBecomeActiveNotification。在这种情况下,不会调用专用选择器。这适用于iOS8。