gpt4 book ai didi

ios - didBecomeActive 发布通知,我的 View Controller 从未听到它

转载 作者:行者123 更新时间:2023-11-28 22:24:23 24 4
gpt4 key购买 nike

应用委托(delegate):

- (void)applicationDidBecomeActive:(UIApplication *)application {
[[NSNotificationCenter defaultCenter] postNotificationName:APP_REFRESH_NOTIFICATION object:nil];
}

在我的 View Controller 中:

- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doStuff) postNotificationName:APP_REFRESH_NOTIFICATION object:self];
}

- (void)doStuff 永远不会被调用。为什么?

最佳答案

我假设您输入的问题不正确,您打算编写 addObserver:selector:name:object:,而不是 addObserver:selector: postNotificationName:object:(不存在这样的方法)

的文档中- (void)addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(id)notificationSender我们可以阅读:

notificationSender

The object whose notifications the observer wants to receive; that is, only notifications sent by this sender are delivered to the observer. If you pass nil, the notification center doesn’t use a notification’s sender to decide whether to deliver it to the observer.

所以在您的情况下,当您在 postNotificationName:object: 中传递 object:nil 时,您还必须设置 object:niladdObserver:selector:name:object: 中。

根据文档,您还应该将方法 doStuff 替换为:

- (void)doStuff:(NSNotification *)notification

并在 addObserver:selector:name:object: 中使用 @selector(doStuff:)

关于ios - didBecomeActive 发布通知,我的 View Controller 从未听到它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19391062/

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