当用户进入后台时,我试图让我的游戏暂停(同时保存游戏的当前状态,如计时器值和分数)。我知道我必须在- (void)applicationDidEnterBackground:(UIApplication *)application
方法
但我不知道该怎么做。我猜我首先必须检查用户当前所在的当前 View Controller 是否是游戏场景,然后,如果是,我调用暂停方法。我有正确的想法吗?我不知道如何访问当前的 View Controller 。任何人都可以解释一下吗?
最佳答案
子类 UINavigationController 并将 View Controller 添加为 UIApplicationDidEnterBackgroundNotification 的观察者
@interface GameViewController : UIViewController
@end
@implementation GameViewController
- (void) init {
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(saveGameState) name: UIApplicationDidEnterBackgroundNotification object: nil];
}
}
- (void) delloc {
[[NSNotificationCenter defaultCenter] removeObserver: self name: UIApplicationDidEnterBackgroundNotification object: nil];
}
- (void) saveGameState {
//do the things you neeed to do
}
@end
关于ios - 进入后台暂停游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24160106/