-1

カウントダウン タイマーが機能しません。画面の「99」で始まり、そこで止まります。まったく動かない。

私のヘッダーファイルで。

@interface FirstTabController : UIViewController {
    NSTimer *myTimer; 
}

@property (nonatomic, retain) NSTimer *myTimer;

私の.mファイルで

- (void)observeValueForKeyPath:(NSString *)keyPath
                  ofObject:(id)object
                    change:(NSDictionary *)change
                   context:(void *)context {
    myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
}

- (void)countDown {
    int counterInt = 100;

    int newTime = counterInt - 1;
    lblCountdown.text = [NSString stringWithFormat:@"%d", newTime];
}

そして、dealloc で「myTimer」を無効にします。だから、誰かが私のコードのどこが悪いのか教えてもらえますか?

4

1 に答える 1

2

タイマー メソッドが呼び出されるたびcounterIntに、100 に設定 (戻る) します。

それを静的変数にすることができます

int counterInt = 100;に変わるstatic int counterInt = 100;

もちろん、デクリメントされた値を counterInt に保存する必要があります。

- (void)countDown {
    static int counterInt = 100;
    counterInt = counterInt - 1;
    lblCountdown.text = [NSString stringWithFormat:@"%d", counterInt];
}

このメソッドの外で変数が必要な場合は、counterInt をクラスのインスタンス変数にする必要があります。

@interface FirstTabController : UIViewController {
    int counterInt;
}

等々。

于 2011-04-04T10:26:07.593 に答える