gpt4 book ai didi

ios - [[NSNotificationCenter defaultCenter] addObserver:选择器:@selector(mymethod)名称:对象:];在iOS 9中不再被调用

转载 作者:行者123 更新时间:2023-12-01 18:49:01 24 4
gpt4 key购买 nike

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

请注意,我在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 - [[NSNotificationCenter defaultCenter] addObserver:选择器:@selector(mymethod)名称:对象:];在iOS 9中不再被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32635586/

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