views:

269

answers:

4

Hi there,

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

A: 

use an int and that should get the job done

Matt S.
This doesn't really answer any question. Use the int where, and how? What would it represent?
Jasarien
use the int on the top (after @synthesize), then when the action is called do i++, then have it call a "checker" that sees if it's above a certain number, if not then add one (++) to the int. and call the check again. Then when the goal is reached have it call whatever you want it to. This way to me looks better in code then grouping it in one big IBAction. This will make the statement false until that goal is reached then you can move on
Matt S.
+2  A: 

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.

Jasarien
+1  A: 

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.

Brandon Bodnár
Using the while loop causes an error at my else statement (syntax error before 'else'). How do I get round this?
Stumf
Think I got it, used IF. I am looking for code to run as long as the difference is >= to 3. The code should check the values until this is the case and then continue. Would using while( (value2ForIf - valueForIf) >= 3 ) {... work?
Stumf
App does nothing when the two values are 3 apart and then proceeds to crash if you decide to press the button again. Any ideas?
Stumf
+1  A: 
- (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:
  }
}
luvieere
Thanks for the in-depth answer. Could you help me further though... how do I declare the progView and the buttonAction?
Stumf
progView is a progress view that you create yourself either in InterfaceBuider, or in code, in a UIView subclass. buttonAction is declared as I've written, just copy-paste the code, and in the .h file add this line: *- (void)buttonAction: (NSTimer *) timer;*
luvieere
Got it working now, exactly what I was looking for... and some! Many thanks luvieere.
Stumf