出现推送通知时暂停游戏
我希望在出现推送通知(推送、短信)时调用我的暂停方法。
首先说一下游戏的概念: 当您启动应用程序时,会显示一个主视图,您可以在其中选择三种不同的游戏模式之一。 因此,如果用户在一种游戏模式下玩游戏并收到短信,则游戏应暂停,如果他取消通知,则游戏应继续。
三种不同模式的视图控制器的名称是PlayViewController、PlayTwoViewController和TimePlayViewController。
所以,这是我的 appDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application { [PlayViewController pauseGame]; [PlayTwoViewController pauseGame];[TimePlayViewController pauseGame]; }
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
}
-(void)applicationDidBecomeActive:(UIApplication *)application { [PlayViewController pauseGame]; [PlayTwoViewController pauseGame];[TimePlayViewController pauseGame]; }
- (void)applicationWillTerminate:(UIApplication *)application {
}
PauseGame 方法始终相同。代码是:
-(IBAction)pauseGame {
[myTimer invalidate];
PauseViewController *screen = [[PauseViewController alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:screen animated:YES];
[screen release]; }
myTimer 只是一个 NSTimer。
当我使用上面的代码构建应用程序时,应用程序崩溃了 控制台显示:
2011-04-03 11:22:10.091 appName[458:207] +[PlayViewController PauseGame]: 无法识别的选择器发送到类 0x1d39c 2011-04-03 11:22:10.093 appName[458:207] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“+[PlayViewController PauseGame]:无法识别的选择器发送到类 0x1d39c” 抛出“NSException”实例后调用终止
I want that my pause method is called when a push notification (push, sms) appears.
First of all, the concept of the game:
When you start the app, a main view is shown where you can choose one of three different game modes.
So, if the user plays in one game mode and gets a sms, the game should pause and if he cancels the notification, the game should go on.
the names of the viewcontrollers of the three different modes are PlayViewController, PlayTwoViewController and TimePlayViewController.
So, here's my appDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application { [PlayViewController pauseGame]; [PlayTwoViewController pauseGame];[TimePlayViewController pauseGame]; }
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
}
-(void)applicationDidBecomeActive:(UIApplication *)application { [PlayViewController pauseGame]; [PlayTwoViewController pauseGame];[TimePlayViewController pauseGame]; }
- (void)applicationWillTerminate:(UIApplication *)application {
}
the pauseGame method is the always the same. The code is:
-(IBAction)pauseGame {
[myTimer invalidate];
PauseViewController *screen = [[PauseViewController alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:screen animated:YES];
[screen release]; }
myTimer is just a NSTimer.
The app crashes when I build it with the code above
The console says:
2011-04-03 11:22:10.091 appName[458:207] +[PlayViewController pauseGame]: unrecognized selector sent to class 0x1d39c
2011-04-03 11:22:10.093 appName[458:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[PlayViewController pauseGame]: unrecognized selector sent to class 0x1d39c'
terminate called after throwing an instance of 'NSException'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实现:
http://developer.apple。 com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html
“调用此方法是为了让您的应用程序知道它即将从活动状态转变为非活动状态。这种情况可能会发生对于某些类型的临时中断(例如来电或 SMS 消息)或当用户退出应用程序并开始转换到后台状态时,处于非活动状态的应用程序将继续运行,但不会将传入事件分派给。 您应该使用此方法来暂停正在进行的任务、禁用
计时器并降低 OpenGL ES 帧速率。处于非活动状态的应用程序在等待转换到任一状态时应该执行最少的工作。活动或后台状态。”
Implement:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html
"This method is called to let your application know that it is about to move from the active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. An application in the inactive state continues to run but does not dispatch incoming events to responders.
You should use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. An application in the inactive state should do minimal work while it waits to transition to either the active or background state."