0
votes

Is this code using UIActivityIndicatorView flawed? It appears that I don't actually get to see the indicator/spinner at all here, so is this because the view isn't drawn until the who viewDidLoad completes?

Is the only way around this to do the viewDidLoad custom work (e.g. data updates) on a separate thread? (I was hoping in this case for an easier single-thread operation). Is there a way to force the view to refresh after the "startAnimating" line perhaps prior to the data loading commencment?

Code from UITableViewController implementation:

- (void)viewDidLoad {
    // Wait indicator - Start
    self.waitView = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];
    self.waitView.hidesWhenStopped = true;
    [self.view addSubview: self.waitView];

    // Load data into tableview
    [NSThread sleepForTimeInterval: 5.0];   // Test code to simulate

    [self.waitView stopAnimating];
}
2
You might want to check the size and origin of the UIActivityIndicatorView.sosborn
what is "load data into tableview"? when is stopAnimating called?bshirley
sorry - will update to the correct code re this - try thatGreg
re - size and origin of the UIActivityIndicatorView - well initially when I had startAnimating instead of stopAnimating (which I fixed) I could see the activity indicator in the top left, so I should be able to see it.Greg

2 Answers

2
votes

You should also call startAnimating. Sleeping is not a good idea. I would prefer the performSelector-methods which starts a not recurring NSTimer under the hood.

Try this:

-(void) doStuff:(id)aSender
{
    [self.waitView stopAnimating];
}
-(void)viewDidLoad
{
    ...
    [self performSelector:@selector(doStuff:) withObject:self afterDelay:5.0];
}

in addtion: also set the frame- or bounds-property of the ActivityIndicatorView somewhere like sosborn said in his comment

1
votes

Actually the answer from Thomas should work as it is, I will add a little explanation as to why not use sleep as you have done it.

All the UI processing on iPhone (and most of OSs as well) is being done in only one thread - the main thread, the thread that executes the so called run loop. If you stop that thread the UI will stop, nothing will be drawn.

Putting sleep into viewDidLoad, which runs in the main thread, will do just that - stop UI from doing anything. So because immediately after wakeup you've called [self.waitView stopAnimating] and the activityview should hide when not animating, you can't see it at all - you just didn't give it any time to show.

Thomas used a NSTimer to call stopAnimating after 5 seconds - now this lets the main thread to execute code before stopping animation and hiding waitView and this will work for your test.

Better yet you just let it animate without any timer and use a delegate patter to be informed by the tableView loading code after the data has been loaded, then stop animating. You don't know how long loading of data will last, so it's better to wait until it's finished than stop animating after any specific time.

Oh well, and the size and position, makes sense, but for testing it doesn't matter and is not the cause of not seeing it - if not specified it will be added at 0,0 and have a default size so you will see it anyway.