我创建了一个游戏...但是当设备关闭时我需要让游戏暂停...当设备关闭时是否有类似viewdidload的动作?三江源
答案 0 :(得分:5)
如果“关闭”是指“睡眠”,则有两种方式:
在您的app delegate中实现这些:
- (void)applicationWillResignActive:(UIApplication *)application
{
// do sleep stuff
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// do wake stuff
}
或注册这些通知:
[[NSNotificationCenter defaultCenter] addObserver:thingThatCares
selector:@selector(whatToDoOnSleep:)
name:UIApplicationWillResignActiveNotification
object:[UIApplication sharedApplication]];
[[NSNotificationCenter defaultCenter] addObserver:thingThatCares
selector:@selector(whatToDoOnWake:)
name:UIApplicationDidBecomeActiveNotification
object:[UIApplication sharedApplication]];
答案 1 :(得分:0)
我会使用app Delegate方法:
- (void)applicationWillTerminate:(UIApplication *)application
暂停游戏。