3

我想在我的应用程序上实现这个功能,但我似乎不知道如何使用这行代码。

- (void)applicationWillResignActive:(UIApplication *)application {
 //our app is going to loose focus since there is an incoming call
 [self pauseGame];
}

- (void)applicationDidBecomeActive:(UIApplication *)application{
 //the user declined the call and is returning to our app
 [self resumeGame];
}

我读过这必须放在 appdelegates 中,但我似乎无法弄清楚当游戏当前在视图控制器中时如何调用我的暂停动作。

谢谢你。

4

2 回答 2

6

与其将消息发送给自己(即应用程序委托),不如将它们发送给您的视图控制器。

例如,如果您的应用程序委托有一个名为“gameViewController”的主游戏视图控制器的属性(其中实现了暂停和恢复的方法):

- (void)applicationWillResignActive:(UIApplication *)application {
    // our app is going to loose focus since there is an incoming call
    [self.gameViewController pauseGame];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // the user declined the call and is returning to our app
    [self.gameViewController resumeGame];
}
于 2010-01-22T04:51:18.140 回答
1

我知道这是很久以前回答的。但我想补充一点,另一个(更可扩展的)解决方案是让相关方(例如,UIViewControllers)注册对 UIApplicationDidEnterBackgroundNotification 和 UIApplicationWillEnterForegroundNotification 的兴趣。

这种方法的优点是应用程序委托不需要直接了解可能需要响应进入背景/前景的对象。

于 2010-11-07T18:41:24.463 回答