I think the problem is that the method aTime
is in your view controller, when you enters another view, this view controller is released and you can't perform selector aTime anymore.
so I suggest that you put your aTime
method and i
to a singleton(or any object that won't be released when you enter another view) and set the singleton as the target of your timer.
also you should keep code below in your view controller so that you can update your label properly when you came back to this view.
-(void)viewDidLoad
{
NSTimer *aTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(aTime) userInfo:nil repeats:YES];
}
-(void)aTime
{
NSLog(@"....Update Function Called....");
Label.text = [NSString stringWithFormat:@"%d",theSingleton.i];
}
better choice:
You can declare i as a property of your singleton, and then add an observer to i, then you'll get your label updated on time.
call -startTimer when you want to count time.
the singleton:
@interface Singleton
@property (nonatomic,retain) NSNumber *i;
@end
@implementation
+(Singleton*)instance
{
//the singleton code here
}
-(void)startTimer
{
NSTimer *aTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(aTime) userInfo:nil repeats:YES];
}
-(void)aTime
{
NSInteger temp = [i integerValue];
temp ++;
self.i = [NSNumber numberWithInteger:temp];
}
the view controller:
-(void)viewDidLoad
{
[super viewDidLoad];
[[Singleton instance] addObserver:self forKeyPath:@"i" options:NSKeyValueObservingOptionNew context:NULL]];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
NSLog(@"....Update Function Called....");
Label.text = [NSString stringWithFormat:@"%@",[Singleton instance].i];
}