I have a UITableViewController, which is RootViewController. It’s XIB only has a table view. I’ve add a UIActivityIndicator through IB and created an IBOutlet. I’ve programmatically added a toolbar with a delete button to the RootViewController. When the user clicks the toolbar’s delete button, I want to display the indicator, centered on the screen. The method below deletes data from a database, which happens very fast. I want to let the user know something is happening. If I run the code below without sleep(), it happens to fast and I don’t see the indicator. But some for some reason, I’m not seeing the indicator anyways. I’m guessing sleep() blocks repainting? If I take out sleep() and the last two lines, I see the indicator but of course it never goes away. It also displays in the top left of the window.
How do I get the code below to work so that the indicator displays for 1/2 – 1 second at least?
How do I align the indicator in the middle of the window?
[self.navigationController.view addSubview:activityIndicator]; [activityIndicator startAnimating]; [activityIndicator setNeedsDisplay]; //do something which might happen really fast sleep(1); //create illusion [activityIndicator stopAnimating]; [activityIndicator removeFromSuperview];
The spinner will not start spinning until you’ve processed the event and returned to the run loop. The way around this is to ask the run loop to finish things up for you.
Here’s an example of what I mean:
Note: there’s nothing in this code that is guaranteed to stop it from processing other touches during the ‘sleep’ period, so make sure you lock out other events somehow.