Tiago_Brasil
Tiago_Brasil

Reputation: 121

iOS - UIAlertView being dismissed before expected

I have a view and, among other things, a button on it. When the user pushes the button, the following code is executed:

- (IBAction) goToPhotoViewControllerView:(id) sender{

    alert = [[UIAlertView alloc] initWithTitle:@"Por favor, aguarde" message:@"Carregando imagens" 
                                  delegate:nil 
                         cancelButtonTitle:nil 
                         otherButtonTitles:nil];
    [alert show];

    if(alert != nil) {
        UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

        indicator.center = CGPointMake(alert.bounds.size.width/2, alert.bounds.size.height-45);
        [indicator startAnimating];
        [alert addSubview:indicator];
        [indicator release];
    }

    dispatch_async(dispatch_get_global_queue(0, 0), ^{

    //Do a lot of stuff here

        PhotoViewController *photoViewControllerView = [[PhotoViewController alloc] initWithNibName:@"PhotoViewController" bundle:nil];

        [iosDao selectComics];

        [photoViewControllerView startWithComic:[[iosDao.comics lastObject] idComic] numPanels:[[[iosDao.comics lastObject] panels] count]];
        photoViewControllerView.navigationItem.title = @"Comics";

        [iosDao release];
        [[self navigationController] pushViewController:photoViewControllerView animated:YES];

        [alert dismissWithClickedButtonIndex:0 animated:YES];
        [alert release];
    });
}

What happens is that, most of the time, the whole asynchronous code runs, dismissing the alert message. However, after the alert message is dismissed, the screen still stays frozen for about 2 or 3 seconds before it pushes to the next viewController (photoViewControllerView). I have no idea why that is happening. I want the alert message to stay on as long as needed. Any ideas?

Thank you in advance!

Upvotes: 1

Views: 509

Answers (2)

Filip Radelic
Filip Radelic

Reputation: 26683

You should just present an alert view and assign self as it's delegate, then when you get informed that alert view is dismissed (via alertView:didDismissWithButtonIndex: delegate method), execute the rest of your code.

Upvotes: 1

pho0
pho0

Reputation: 1751

So to me, it sounds like the problem is the PhotoViewController is taking time to load (can you use Instruments to confirm?). Since PhotoViewController is your own class, you could have it have a delegate, make the main view controller (the one that dismisses the UIAlert) the delegate of PhotoViewController instance, and have the PhotoViewController call the delegate to inform it when it's ready and/or visible, for example at the end of

- (void)viewWillAppear:(BOOL)animated

Upvotes: 1

Related Questions