-1

こんにちは、私は iOS 開発の初心者です。NSTimer に関連する問題が 1 つあります。ビューコントローラーで NSTimer を使用しています。
タイマーは、UIButton イベントで値の減少を開始します。別のView Controllerに移動したときにタイマーを呼び出した後。次に、タイマーが正常に機能することを示す値を減らします。しかし、タイマービューに戻ると、タイマーは値の更新を停止します。これは、タイマーが @selector() メソッドを呼び出さないことを意味します。タイマー ビューに戻ったときに値更新メソッドを呼び出すにはどうすればよいですか?

私のコードは次のとおりです。

-(IBAction)btnStrDecPressed:(id)sender
{

    countDownlabel.text = [NSString stringWithFormat:@"%@:%@:%@", pickhour, pickmins, picksecs];
    secondsLeft=[pickhour intValue] * 3600;
    secondsLeft=secondsLeft + [pickmins intValue] * 60;
    secondsLeft=secondsLeft + [picksecs intValue];
    appdel.LeftSeconds=secondsLeft;
    NSLog(@"%d",appdel.LeftSeconds);


   if(timer==nil)
   {
       timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];
   }
 }

-(void)updateCountdown
{
  int hours, minutes, seconds;

  hours = appdel.LeftSeconds / 3600;
  minutes = (appdel.LeftSeconds % 3600) / 60;
  seconds = (appdel.LeftSeconds %3600) % 60;
  appdel.LeftSeconds--;
  countDownlabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];

  if (appdel.LeftSeconds==0)
  {
      [[[UIAlertView alloc] initWithTitle:@"Restaurant" message:@"Timer Completed" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
      [timer invalidate];
  }
}

pickhour、pickmins、picksecs は、UIPickerview から取得する値です。

4

3 に答える 3

1

次のコードを使用します。

-(void)viewWillAppear:(BOOL)animated
{
    timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];
}

-(void)updateCountdown
{
  NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
  int hours, minutes, seconds;
  if([def valueForKey:@"time"] != nil)
  {
     countDownlabel.text = [def valueForKey:@"time"];
  NSArray *arr = [countDownlabel.text componentsSeparatedByString:@":"];
  hours = [arr objectAtIndex:0];
  minutes = [arr objectAtIndex:1];
  seconds = [arr objectAtIndex:2];
  appdel.LeftSeconds--;
  }
  else{
  hours = appdel.LeftSeconds / 3600;
  minutes = (appdel.LeftSeconds % 3600) / 60;
  seconds = (appdel.LeftSeconds %3600) % 60;
  appdel.LeftSeconds--;
  }
  countDownlabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
  [def setValue:[NSString stringWithFormat:@"%@",countDownlabel.text] forKey:@"time"];
  if (appdel.LeftSeconds==0)
  {
      [[[UIAlertView alloc] initWithTitle:@"Restaurant" message:@"Timer Completed" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
      [timer invalidate];
  }
}

(注: NSUserdefault 値を使用して時刻を保存します。ビューが表示されると、デフォルトで保存された値が取得されます)

于 2013-08-02T09:44:31.130 に答える
0

問題は、タイマー ビュー コントローラーを離れて戻ってくるたびに、その新しいインスタンスを作成することです。そのため、タイマーは毎回 nil であり、タイマーを開始するたびに 1 秒ずつ減少します。

updateCountdownセレクターとタイマーを AppDelegate に移動することをお勧めします。

于 2013-08-02T10:51:08.353 に答える
0

タイマーは、ビューが表示されているときだけでなく、[タイマーが無効になる]まで常に刻々と進んでいます。

再初期化できない理由は、タイマーを完全にリセットしていないためです。試す:

[timer invalidate];
timer = nil;
于 2013-08-02T09:47:24.930 に答える