开发者

Loop Until Condition Reached, iPhone

I have a problem here...

After a button is pressed I want a loop to run until a condition is reached:

- (IBAction)buttonclick1 ...

if ((value2ForIf - valueForIf) >开发者_运维问答;= 3) { ...

I would like a loop to run until

((value2ForIf - valueForIf) >= 3)

and then execute the code associated with the IF statement.

What I am aiming to achieve is the program to keep checking if the above statement is true, before continuing with the code. On top of this there is an else statement beneath the IF, though I don't know if this will affect a loop.

I am unsure of the format of the loop required here and everything I have tried has caused errors. Any help would be greatly appreciated.

Stu


- (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:
  }
}


Rather than run a tight loop, which would block your app's execution unless run on another thread, you could use an NSTimer to call a method at a time interval of your choosing and check the condition in that method. If the condition is satisfied, you can invalidate the timer and continue.


From what you stated, what you want is a while loop

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

This will run the code in the braces as long as the difference in values is less than 3, meaning it will run until their difference is 3 or greater. But as Jasarien said. This is a bad idea since you will be blocking your program. If the values are being updated by the code itself that is fine. But if they are being updated by some UI from the user, your while loop will block the UI and not allow the user to input anything.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜