iOS - UIAlertView在预期之前被解雇

时间:2011-08-16 17:54:23

标签: objective-c ios multithreading uialertview dispatcher

我有一个视图,除此之外,还有一个按钮。当用户按下按钮时,执行以下代码:

- (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];
    });
}

在大多数情况下,整个异步代码会运行,并且会解除警报消息。但是,在警报消息被解除后,屏幕仍会保持冻结约2或3秒,然后才会推送到下一个viewController(photoViewControllerView)。我不知道为什么会这样。我希望警报消息在需要时保持不变。有什么想法吗?

提前谢谢!

2 个答案:

答案 0 :(得分:1)

所以对我而言,听起来问题是PhotoViewController需要时间来加载(你可以使用Instruments来确认吗?)。由于PhotoViewController是你自己的类,你可以让它拥有一个委托,使主视图控制器(解散UIAlert的那个)成为PhotoViewController实例的委托,并让PhotoViewController调用委托,以便在它准备好时通知它和/或可见,例如在

的末尾
- (void)viewWillAppear:(BOOL)animated

答案 1 :(得分:1)

您应该只提供一个警报视图并指定self作为其委托,然后当您收到警报视图被解除时(通过alertView:didDismissWithButtonIndex:委托方法),请执行其余代码。