gpt4 book ai didi

ios - 从应用委托(delegate)调用实例函数

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

我有一个名为 game.h 的类,它有一个名为 pause 的实例方法。当我的游戏进入后台时,如何从应用程序委托(delegate)中调用它?

我知道你用 - (void)applicationWillResignActive:(UIApplication *)application ,但我想在我现有的实例上调用暂停。

最佳答案

使用 NSNotificationCenter .

在您的 game 实例中类,添加self作为 UIApplicationWillResignActiveNotification 的观察员通知。

在您的 game类,某处,您需要以下代码段:

[[NSNotificationCenter defaultCenter] 
addObserver:self
selector:@selector(pause)
name:UIApplicationWillResignActiveNotification
object:nil];

这可能应该进入 init .

此通知由 applicationWillResignActive 触发. addObserver:selector:name:object:方法将您的对象设置为在收到通知时调用您告诉它的选择器。

不要忘记删除 self作为 dealloc 中的观察员.

game.m@implementation :
- (void)dealloc {
// If not using ARC, then be sure to [super dealloc];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

关于ios - 从应用委托(delegate)调用实例函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21567719/

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