0

ここに問題があります...

ボタンが押された後、条件に達するまでループを実行したい:

- (IBAction)buttonclick1 ...

if ((value2ForIf - valueForIf) >= 3) { ...

までループを実行したい

((value2ForIf - valueForIf) >= 3)

次に、IFステートメントに関連付けられたコードを実行します。

私が達成しようとしているのは、コードを続行する前に、上記のステートメントが真であるかどうかをチェックし続けるプログラムです。これに加えて、IFの下にelseステートメントがありますが、これがループに影響するかどうかはわかりません。

ここで必要なループの形式がわかりません。試したすべての結果でエラーが発生しました。どんな助けでも大歓迎です。

Stu

4

3 に答える 3

2

別のスレッドで実行しない限りアプリの実行をブロックするタイトループを実行するのではなく、NSTimerを使用して、選択した時間間隔でメソッドを呼び出し、そのメソッドの条件を確認できます。条件が満たされた場合は、タイマーを無効にして続行できます。

于 2009-11-16T00:38:41.423 に答える
2
- (IBAction)buttonclick1 ...
{
  //You may also want to consider adding a visual cue that work is being done if it might
  //take a while until the condition that you're testing becomes valid.
  //If so, uncomment and implement the following:

  /*
   //Adds a progress view, note that it must be declared outside this method, to be able to
   //access it later, in order for it to be removed
   progView = [[MyProgressView alloc] initWithFrame: CGRectMake(...)];
   [self.view addSubview: progView];
   [progView release];

   //Disables the button to prevent further touches until the condition is met,
   //and makes it a bit transparent, to visually indicate its disabled state
   thisButton.enabled = NO;
   thisButton.alpha = 0.5;
  */

  //Starts a timer to perform the verification
  NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 0.2
                            target: self
                            selector: @selector(buttonAction:)
                            userInfo: nil
                            repeats: YES];
}


- (void)buttonAction: (NSTimer *) timer
{
  if ((value2ForIf - valueForIf) >= 3)
  {
    //If the condition is met, the timer is invalidated, in order not to fire again
    [timer invalidate];

    //If you considered adding a visual cue, now it's time to remove it
    /*
      //Remove the progress view
      [progView removeFromSuperview];

      //Enable the button and make it opaque, to signal that
      //it's again ready to be touched
      thisButton.enabled = YES;
      thisButton.alpha = 1.0;
    */

    //The rest of your code here:
  }
}
于 2009-11-16T09:08:11.993 に答える
1

あなたが言ったことから、あなたが欲しいのはwhileループです

while( (value2ForIf - valueForIf) < 3 ) { ...Code Here... }

これにより、値の差が3未満である限り、中括弧でコードが実行されます。つまり、値の差が3以上になるまで実行されます。しかし、ジャサリエンが言ったように。プログラムをブロックするので、これは悪い考えです。値がコード自体によって更新されている場合は問題ありません。ただし、ユーザーからのUIによって更新されている場合、whileループはUIをブロックし、ユーザーが何も入力できないようにします。

于 2009-11-16T00:41:28.847 に答える