English 中文(简体)
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.





相关问题
Find the closest time from a list of times

So, here s the scenario. I have a file with a created time, and I want to choose a time from a list of times that that file s created time is closest or equal too...what would be the best way to ...

PHP Comparison Operators and Data Types

I m currently working through O Reilly s "Programming PHP" and have come across this table titled "Type of comparison performed by the comparison operators": First Operand | Second ...

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 ...

nuSoap or Zend Soap? [closed]

I would like to know the differences between nusoap and ZendSoap, which is the best? what benefits and disadvantages of each? Anyone who has used both technologies could make this comparison? Thank ...

Logic for a file compare

I trying to write a programm for file compare. For example: file1 1 2 3 4 5 file2 1 2 @ 3 4 5 If I do it line by line, I get: 1 == 1; 2 == 2; 3 != @; 4 != 3; 5 != 4; != 5; But, the truth is ...

热门标签