1

最近、ストップウォッチ アプリケーションを作成しましたが、いくつかの不具合がありました。

停止ボタンを 2 回続けて押すと、アプリ全体がクラッシュします。

開始ボタンを 2 回続けて押すと、タイマーが 2 倍の速さで実行され、停止ボタンが機能しなくなります。

この問題を解決するにはどうすればよいですか?

.h ファイルのコードは次のとおりです。

    IBOutlet UILabel *time;
    IBOutlet UILabel *time1;
    IBOutlet UILabel *time2;

    NSTimer *myTicker;
    NSTimer *myTicker2;
    NSTimer *myTicker3;
}

- (IBAction)start;
- (IBAction)stop;
- (IBAction)reset;


- (void)showActivity;
- (void)showActivity1;
- (void)showActivity2;

@end

そして、ここに.mファイルの私のコードがあります:

- (IBAction)start {
    myTicker = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(showActivity) userInfo:nil repeats:YES];  

    myTicker2 = [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(showActivity1) userInfo:nil repeats:YES];

    myTicker3 = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(showActivity2) userInfo:nil repeats:YES];        
}

- (IBAction)stop {
    [myTicker invalidate];
    [myTicker2 invalidate];
    [myTicker3 invalidate];
}

- (IBAction)reset {    
    time.text = @"00";
    time1.text = @"00";
    time2.text = @"00";
}

- (void)showActivity {    
    int currentTime = [time.text intValue];
    int newTime = currentTime + 1;
    if (newTime == 60) {
        newTime = 0;
    }
    time.text = [NSString stringWithFormat:@"%d", newTime];     
}

- (void)showActivity1 {
    int currentTime1 = [time1.text intValue];
    int newTime1 = currentTime1 + 1;
    if (newTime1 == 10) {
        newTime1 = 0;
    }
    time1.text = [NSString stringWithFormat:@"%d", newTime1];    
}

- (void)showActivity2 {
    int currentTime2 = [time2.text intValue];
    int newTime2 = currentTime2 + 1;
    time2.text = [NSString stringWithFormat:@"%d", newTime2];
}
4

2 に答える 2

1

プライベートBOOL変数「isRunning」を作成する必要があります。これは、次のように「停止」または「開始」をクリックするとチェックされます。

- (IBAction)stop {
    if(!isRunning) return;

    [myTicker invalidate];
    [myTicker2 invalidate];
    [myTicker3 invalidate];

    self.isRunning = NO;
}

また、ユーザーの操作を無視することも一般的には良い考えですが(CodaFiが提案したように)、症状と戦うだけです;-)実際には両方のチェックを行う必要があります。

于 2012-07-08T17:28:13.433 に答える
1

メソッドが起動されたときに、停止ボタンのuserInterActionEnabledプロパティを に設定NOし、開始ボタンのプロパティを に設定します。次に、停止ボタンのtoと起動ボタンの to を切り替えて設定します。YES-stopuserInterActionEnabledYESNO-start

于 2012-07-08T17:15:01.370 に答える