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 に答える