0

当您导航到不同的页面时,我NSTimer需要在后台运行它,但是当我在视图控制器文件中拥有它时,id 会移动到另一个页面,它实际上会破坏计时器。所以我做了一个单例类并试图把代码放在那里,然后从那里调用它。此时代码运行,但当我导航到另一个页面时它不会继续运行计时器。任何想法都非常感谢,请询问更多信息!

代码:Viewcontroller.h

#import <UIKit/UIKit.h>
#import "ApplicationManager.h"
@interface ViewController : UIViewController{

IBOutlet UILabel *time;
NSTimer *ticker;
}

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


- (void)showActivity;


@end
//
//  ViewController.m
//  License
//
//  Created by Connor Gosell on 7/2/13.
//  Copyright (c) 2013 Connor Gosell. All rights reserved.
//

#import "ViewController.h"
#import "ApplicationManager.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
/*-(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.h
//  License
//
//  Created by Connor Gosell on 7/31/13.
//  Copyright (c) 2013 Connor Gosell. All rights reserved.
//

#import <Foundation/Foundation.h>
@interface ApplicationManager : NSObject{

}

+(ApplicationManager*) instance;



@end
//
//  ApplicationManager.m
//  License
//
//  Created by Connor Gosell on 7/31/13.
//  Copyright (c) 2013 Connor Gosell. All rights reserved.
//
#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;
}
4

2 回答 2

0

我会做一些假设,但是您可以根据系统时间而不是通过增加 var 来计算时间。如果整个应用程序重用开始时间,则可以将其作为属性存储在应用程序委托中。如果以这种方式计算时间,则计时器可以在每个视图控制器出现时启动并在消失时停止,如果需要准确性,可以从更快的间隔中受益。我在 Pace Clock Mac 和 iOS 应用程序中使用了与此类似的技术。

于 2013-08-03T08:13:38.303 回答
0

将计时器放在您的 AppDelegate 中,让计时器滴答声发送通知,让您的每个视图控制器为通知启用一个侦听器,以便他们可以进行更新。

于 2013-08-03T11:59:07.240 回答