iPhone UIActivityIndicator threaded help!
I'm looking to create a generic thread in my main delegate that I can use to display a UIActivityIndicator. I have several views that push new views onto the stack. Most of the views are called from the didSelectRow method in the calling view. When they select a row in a UITableView, I want to start the Activity Indicator and push the new view onto the stack. In the new view's viewDidLoad method, I make several calls to a server using Json which can take some time to process. What is the best way to show an Activity indicator from view1, and hide it in a view2 after the processing is complete?
Am I dreaming that I could make 2 methods in my main delegate?
and call [startIndicator] from view1 which would show the Activity Indicator over top of all views.
and call [stopIndicator] from view2 after all processing the viewDidLoad method which would hide the Activi开发者_开发知识库ty Indicator
Thanks for you help!
Yes, you could, just make sure to call any UIKit routines on the main thread. You can make it more "thread friendly" by writing the routines like:
-(void)startIndicator {
if ([NSThread isMainThread] == FALSE) {
[self performSelectorOnMainThread:@selector(startIndicator) withObject:nil waitUntilDone:NO];
return;
}
/* Actual UIIndicator setup */
}
If you have several threads that may start or stop the indicator, you may also want to add a reference count to the routines, incrementing every time some thread calls "start" and decrementing when they call "stop", then keep the Activitiy Indicator going until a "stop" call reduces the count to zero.
精彩评论