0

I want to present an alert view after the user click on a button at 2s. The function is to start calibration, and I want to show the user that calibration is done after 2s.

I'm counting in an array that when it reaches 20 (presents 2s), and I used

if showCalibDone {

    let calibDoneAlert = UIAlertController(title: "", message: "Calibration is finished", preferredStyle: .Alert)

    calibDoneAlert.addAction(UIAlertAction(title: "", style: .Default, handler: {(action: UIAlertAction!) in

        self.x0 = self.average40(self.xCalibrate)

        self.presentViewController(calibDoneAlert, animated: true, completion: nil)

    }))

This is the counting. xCalibrate is an array which will add an object each time the accelerometer updates.

if xCalibrate.count == 40 {

                println("40 achieved")
                self.showCalibDone = true
}

But the alert view never appears with this if condition. I tried to put it in viewWillAppear or viewDidLoad, but it seems both are not correct.

How can I get this 'loop' be executed? Where should I put it? Or maybe I should use a timer to count?

Popeye
  • 10,831
  • 9
  • 52
  • 85
Lynn_Yang
  • 157
  • 1
  • 10
  • Where is your `UIAlertView`? Do you mean `UIAlertController`? Because that is what your code says you are using? – Popeye Mar 20 '15 at 11:04

2 Answers2

1

If you just want some code to be executed after some a delay, use dispatch_after:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue()) {
    // ...
}
Rengers
  • 13,246
  • 1
  • 32
  • 49
0

You can also use performSelector to achieve the same results

- (void) showAlert
{
  [[[UIAlertView alloc] initWithTitle:@"Title" message:@"Some message" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK",nil] show];
}

//

[self performSelector:@selector(showAlert) withObject:nil afterDelay:2.0f];
Nitheesh George
  • 1,229
  • 10
  • 13