ios - 导航时保持计时器在另一个页面上运行
<p><p>当您切换到其他页面并完成其他任务时,我试图让计时器在另一个页面上运行,实质上是在记录完成任务所需的时间。每当我切换到另一个页面时,它都会将计时器重置为开始时的状态,并且对我试图保持打开的其他页面上的一些开关执行相同的操作。有任何想法吗? </p>
<p> Storyboard截图:</p>
<p> <img src="/image/B9bha.png" alt=""/> </p>
<p>到目前为止的代码:</p>
<pre><code>//
//ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (IBAction)start{
ticker = ;
}
- (IBAction)reset{
;
time.text = @" 0:00";
}
- (void)showActivity{
int currentTime = ;
int newTime = currentTime + 1;
time.text = ;
}
- (void)viewDidLoad
{
;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
;
// Dispose of any resources that can be recreated.
}
@end
</code></pre>
<hr/>
<pre><code>//ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController{
IBOutlet UILabel *time;
NSTimer *ticker;
}
- (IBAction)start;
- (IBAction)reset;
- (void)showActivity;
@end
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您的 NSTimer 是您的 ViewController 类的成员变量。我假设当你在 View 之间切换时,你正在破坏这个 ViewController 并实例化一个新的实例。这意味着这个 ViewController 和计时器都不见了;不是计时器被重置,而是您的旧计时器已被销毁,正在创建一个新计时器。</p>
<p>您需要将 NSTimer 及其功能存储在每次更改 ViewController 时都不会被破坏的地方。一种解决方案是创建一个处理计时器的 Singleton 类。 (Singleton 类是一个只能被创建一次的类;它只能存在一个实例。您可以阅读更多关于它们的信息 <a href="http://en.wikipedia.org/wiki/Singleton_pattern" rel="noreferrer noopener nofollow">here</a>。)</p>
<p>这是一个如何在 Objective-C 中创建 Singleton 类的示例。标题:</p>
<pre><code>//ApplicationManager.h
@interface ApplicationManager : NSObject
+(ApplicationManager*) instance;
@end
</code></pre>
<p>以及实现:</p>
<pre><code>//ApplicationManager.m
#import "ApplicationManager.h"
@implementation ApplicationManager
static ApplicationManager* appMgr = nil;
+(ApplicationManager*) instance
{
@synchronized()
{
if(!appMgr)
{
appMgr = [ init];
}
return appMgr;
}
return nil;
}
+(id) alloc
{
@synchronized()
{
NSAssert((appMgr == nil), @"Only one instance of singleton class may be instantiated.");
appMgr = ;
return appMgr;
}
}
-(id) init
{
if(!(self = ))
{
;
return nil;
}
return self;
}
@end
</code></pre>
<p>第一次调用 <code>instance</code> 方法时,ApplicationManager 的实例将被创建。每次要访问时,再次调用<code>instance</code>方法; ApplicationManager 将被返回。现在您只需将您的 NSTimer(以及您希望在整个应用程序中保留的任何其他对象)添加为 ApplicationManager 类的成员变量。</p>
<p>然后您必须将 ApplicationManager 类导入您的 ViewController 类,您的 ViewController 方法将更改为:</p>
<pre><code>-(IBAction) start
{
[ setTicker:];
}
-(IBAction) reset
{
[[ ticker] invalidate];
time.text = @" 0:00";
}
-(void) showActivity
{
int currentTime = ;
int newTime = currentTime + 1;
time.text = ;
}
</code></pre>
<p>如果你想让事情变得漂亮整洁,你也可以在你的 ApplicationManager 类的顶部添加这一行:</p>
<pre><code>#define APPMGR
</code></pre>
<p>现在不必在任何地方键入 <code></code>,您可以简单地将其称为 <code>APPMGR</code>。 <code></code> 比 <code>[ ticker]</code> 干净得多 :)</p></p>
<p style="font-size: 20px;">关于ios - 导航时保持计时器在另一个页面上运行,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/18026723/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/18026723/
</a>
</p>
页:
[1]