0

I have the NSTimer that needs to be run in the background as you navigate around to different pages, but when I had that in the view controller files, id move to another page and it would essentially destroy the timer. so I made a singleton class and attempted to put the code in there and just call it from there. at this point the code runs, but it doesn't continue to run the timer when I navigate to another page. any ideas are greatly appreciated, please ask for more info!

Code: 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