导航时保持计时器在另一个页面上运行 [英] Keeping a timer running on another page when navigating around

查看:51
本文介绍了导航时保持计时器在另一个页面上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您切换到其他页面并完成其他任务时,我试图让计时器在另一个页面上运行,实质上是记录完成任务所需的时间.每当我切换到另一个页面时,它都会将计时器重置回它开始时的状态,并且对其他页面上我试图保持的某些开关执行相同的操作.有任何想法吗?

I'm trying to keep a timer running on another page when you switch to other pages and complete other tasks, in essence keeping a clock on how long it takes to do the tasks. Whenever I switch to another page, it resets the timer back to what it was started, and does the same with some switches on other pages that I'm trying to keep on. Any ideas?

故事板截图:

到目前为止的代码:

//
//  ViewController.m


#import "ViewController.h"

@interface ViewController ()


@end

@implementation ViewController

- (IBAction)start{
ticker = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self    ``selector:@selector(showActivity) userInfo:nil repeats:YES];

}

- (IBAction)reset{
[ticker invalidate];
time.text = @" 0:00";
}

- (void)showActivity{
int currentTime = [time.text intValue];
int newTime = currentTime + 1;
time.text = [NSString stringWithFormat:@"%d", newTime];
}




- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

<小时>

//  ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{

IBOutlet UILabel *time;
NSTimer *ticker;
}

- (IBAction)start;
- (IBAction)reset;


- (void)showActivity;
@end

推荐答案

你的 NSTimer 是你的视图控制器类的成员变量.我假设当你在视图之间切换时,你正在破坏这个视图控制器并实例化一个新的实例.这意味着这个视图控制器和计时器都消失了;不是计时器正在重置,而是您的旧计时器已被销毁,正在创建新计时器.

Your NSTimer is a member variable of your view controller class. I'm assuming that when you switch between views, you're destroying this view controller and instantiating an instance of a new one. That means this view controller is gone, as well as the timer; it's not that the timer is being reset, it's that your old timer has been destroyed an a new one is being created.

您需要的是将 NSTimer 及其功能存储在每次更改视图控制器时都不会被破坏的地方.一种解决方案是创建一个处理计时器的 Singleton 类.(单例类是一种只能创建一次的类;它只能存在一个实例.您可以阅读有关它们的更多信息 此处.)

What you need is to store your NSTimer and its functionality in a place where it will not be destroyed every time you change your view controller. One solution is to create a Singleton class which handles the timer. (A Singleton class is a class that can only be created one time; only one instance of it can exist. You can read more about them here.)

这是一个如何在 Objective-C 中创建单例类的示例.标题:

Here is an example of how you can create a Singleton class in Objective-C. The header:

//ApplicationManager.h

@interface ApplicationManager : NSObject

+(ApplicationManager*) instance;

@end

和实现:

//ApplicationManager.m

#import "ApplicationManager.h"

@implementation ApplicationManager
static ApplicationManager* appMgr = nil;

+(ApplicationManager*) instance
{
    @synchronized([ApplicationManager class])
    {
        if(!appMgr)
        {
            appMgr = [[self alloc] init];
        }

        return appMgr;
    }

    return nil;
}    

+(id) alloc
{
    @synchronized([ApplicationManager class])
    {
        NSAssert((appMgr == nil), @"Only one instance of singleton class may be instantiated.");
        appMgr = [super alloc];
        return appMgr;
    }
}

-(id) init
{
    if(!(self = [super init]))
    {
        [self release];
        return nil;
    }

    return self;
}

@end

第一次调用 instance 方法时,会创建 ApplicationManager 的实例.每次要访问时,再次调用instance方法;ApplicationManager 将被返回.现在,您只需将 NSTimer(以及您希望在整个应用程序中保留的任何其他对象)添加为 ApplicationManager 类的成员变量.

The first time you call the instance method, the instance of the ApplicationManager will be created. Each time you want to access it, call the instance method again; the ApplicationManager will be returned. Now you simply add your NSTimer (and any other object you wish to persist throughout your application) as member variables of the ApplicationManager class.

然后您必须将 ApplicationManager 类导入到您的视图控制器类中,您的视图控制器方法将更改为:

You then must import the ApplicationManager class into your view controller class, and your view controller methods will change to:

-(IBAction) start
{
    [[ApplicationManager instance] setTicker:[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(showActivity) userInfo:nil repeats:YES]];
}

-(IBAction) reset
{
    [[[ApplicationManager instance] ticker] invalidate];
    time.text = @" 0:00";
}

-(void) showActivity
{
    int currentTime = [time.text intValue];
    int newTime = currentTime + 1;
    time.text = [NSString stringWithFormat:@"%d", newTime];
}

如果你想让事情变得漂亮和整洁,你也可以在你的 ApplicationManager 类的顶部添加这一行:

If you want to make things nice and neat, you can also add this line to the top of your ApplicationManager class:

#define APPMGR [ApplicationManager instance]

现在不必在任何地方键入 [ApplicationManager instance],您只需将其称为 APPMGR.[APPMGR ticker][[ApplicationManager instance] ticker] 干净多了 :)

Now instead of having to type [ApplicationManager instance] everywhere, you can simply refer to it as APPMGR instead. [APPMGR ticker] is a lot cleaner than [[ApplicationManager instance] ticker] :)

这篇关于导航时保持计时器在另一个页面上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆