0

我试图制作一个从 30 倒计时到 0 的计时器,但这是我能想到的让它工作的唯一方法,但它不起作用。有谁知道我做错了什么?

.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];

}
}
4

1 回答 1

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
于 2012-05-04T19:34:08.987 回答