2012-05-04 1 views
0

Im пытается сделать таймер, который отсчитывает от 30 до 0, но это единственный способ, которым я мог подумать, чтобы он работал, но он не работает. Кто-нибудь знает, что я делаю неправильно?NSTimer/NSTimeInterval

.h файл

@interface countDownAppViewController : UIViewController { 

UIButton *countDown; 
UILabel *displayThis; 
} 

@property (nonatomic, retain) IBOutlet UIButton *countDown; 
@property (nonatomic, retain) IBOutlet UILabel *displayThis; 

-(IBAction) theCount:(id) sender; 
-(IBAction) displayStuff:(id) sender; 

@end 

.m файл

@synthesize countDown; 
@synthesize displayThis; 

-(IBAction) theCount:(id) sender { 
[NSTimer scheduledTimerWithTimeInterval:1.0 
           target:self 
           selector:@selector(displayStuff:) 
           userInfo:nil 
           repeats:NO]; 

} 
int batman=30; 
-(void) viewDidLoad{ 

displayThis.text = [NSString stringWithFormat:@"%i",batman]; 
} 

-(IBAction) displayStuff:(id) sender { 
while (batman >= 0){ 
    batman--; 
    [NSTimer scheduledTimerWithTimeInterval:1.0 
            target:self 
            selector:@selector(displayStuff:) 
            userInfo:nil 
            repeats:NO]; 
displayThis.text = [NSString stringWithFormat:@"%i",batman]; 

} 
} 

ответ

0

Вы пробовали писать ее так, как она на самом деле должны были написаны? Аргумент repeats существует именно для этой цели. Вы можете написать такой метод:

@interface Whatever: UIViewController 
{ 
    NSTimer *timer; 
    int count; 
    int maxCount; 
} 

- (void)countDownFrom:(int)cnt; 

@end 

@implementation Whatever 

- (void)countDownFrom:(int)cnt 
{ 
    maxCount = cnt; 
    count = 0; 
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 
           target:self 
           selector:@selector(doCount) 
           userInfo:nil 
           repeats:YES]; 
} 

- (void)doCount 
{ 
    count++; 
    textField.text = [NSString stringWithFormat:@"Count: %d", count]; 
    if (count >= maxCount) 
    { 
     [timer invalidate]; 
    } 
} 

@end