I am developing an iOS 5.1 application on Xcode 4.2.
I have a uitablcontroller with different tabs. My problem is when a tab is clicked , the application ‘freezes’ for few seconds and does all the codes it’s meant to do, but it does not load the UIAlertView first as it should be.
I have the UIAlertView declared in the viewDidLoad.
Here is a code snippet:
- (void)viewDidLoad
{
NSLog(@"##### VIEW DID LOAD 1 #####");
// Display Alert: Loading
alertView = [[UIAlertView alloc] initWithTitle:@"Loading"
message:@"\n"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinner.center = CGPointMake(139.5, 75.5); // .5 so it doesn't blur
[alertView addSubview:spinner];
[spinner startAnimating];
[alertView show];
[super viewDidLoad];
NSLog(@"##### VIEW DID LOAD 2 #####");
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self callMainMethod];
}
When the tab is clicked, I can see that the first NSLog’s are displayed in the Log, and then the main method is called, but the UIAlertview is not displayed.
When
viewDidLoadis running, it may be that theframeof the associated UIView has zero size. I don’t know, but theUIAlertViewmay be trying to present itself in this zero-size frame. Does it make sense if you present theUIAlertViewinviewDidAppear?If
[self callMainMethod]is taking lots of compute power, then the display might not be updated until it finishes. You could try moving it toviewDidAppear. You could also try delaying it, so that the main run loop for the UI thread, the thread that the display is updated on and the thread that executesviewDidLoadand all the otherview...methods, has time to complete everything and become idle before you start the heavy processing. It’s only when the run loop has done all the processing it can that it starts actually to update the display. Like this:The documentation for that is at http://developer.apple.com/library/ios/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html
If that suspicion is correct, you should then get the
UIAlertViewpopping straight up, only to have everything go dead for a while, whilecallMainMethodexecutes.I wish I knew how to write, “dispatch this block only after you’ve managed to finish updating the display with everything up to here”, but I don’t know how to do that. So the dispatch call above should delay the call to
callMainMethodby 200ms, which is usually plenty.If that works, you should probably start another question, something like “How can I stop the display freezing while I execute this method.”