• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

从 xcode 和目标 c 的 appdelegate 暂停计时器

用户头像
it1352
帮助1

问题说明

我正在为 iPhone 制作游戏,并且希望能够在用户的游戏中断时(例如他们按下主页按钮时)暂停计时器.我知道在应用程序委托中有一个当应用程序离开前台时调用的方法:

I am making a game for iPhone and want to be able to pause a timer when a user's game is interrupted like when they hit the home button. I know that in the app delegate there is a method when the app leaves the foreground called:

    - (void)applicationWillResignActive:(UIApplication *)application

我正在努力解决的是如何暂停计时器.我的视图控制器中有一个名为 pauseGame 的函数,用于当用户想要暂停游戏时.我认为使用这种方法暂停游戏是最简单的.但是,我无法理解如何调用此方法.有任何想法吗?很抱歉初学者的问题.

What I am struggling with is how to pause the timer. I have a function in my view controller that's called pauseGame and is used for when the user wants to pause the game. I was thinking that it would be easiest to pause the game by using this method. I cannot however understand how to call this method. Any ideas? And sorry for the beginner question.

正确答案

#1

最短的方法是使用通知:

The shortest way is to Use Notifications:

1. 在您的应用程序委托(或其他任何地方...)定义自定义通知

1. define a custom notification, at your application delegate (or anywhere else...)

#define kApplicationWillResignActiveNotification  
@"kApplicationWillResignActiveNotification"

2. 在 applicationWillResignActive: 方法被调用时发送通知

2. dispatch the notification when the applicationWillResignActive: method is called

[[NSNotificationCenter defaultCenter] postNotificationName:
kApplicationWillResignActive object:nil];

3. 在项目中的任何位置收听该通知(* 在您@定义通知的位置导入头文件)

3. listen to that notification where ever you want in your project (* import the header file where you @defined the notification)

[[NSNotificationCenter defaultCenter] addObserver:self
selector: @selector(appResigned:) 
name:kApplicationWillResignActiveNotification object: nil];

4.如果将 NSNotification 对象添加到选择器中,您可以获得它

4. you can get the NSNotification Object if you add it to your selector

-(void)appResigned:(NSNotification *)notification;

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfjjee
系列文章
更多 icon
同类精品
更多 icon
继续加载