I’m trying to make a timer by myself but it doesn’t work. It seems like it goes nil every time it repeats, anyone? It works on OS X but not on iOS.
I just need a working loop method – any suggestions?
Header:
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UILabel *timerOutlet;
- (void)timer:(id)sender;
@end
Implementation:
@synthesize timerOutlet;
- (void)viewDidLoad
{
[super viewDidLoad];
started = TRUE;
[self timer:nil];
}
- (void)timer:(id)sender {
int test = 1;
NSString *number = [NSString stringWithFormat:@"%i",test];
timerOutlet.text = number;
[self performSelector:@selector(timer:) withObject:Nil afterDelay:1.0];
test ++;
}
The problem is that you re-declare
testand assign 1 to it every time the timer method executes. So if you either omit the declaration and maketestan instance variable, or make itstatic, that should help. Apart from that, however, you should really use an NSTimer object instead of raping the framework.