我在后台运行下载。它显示了一些失败条件下的UIAlertView。
当发出此警报时,应用程序可以位于其向用户显示的任何视图中,但只应在其中一个中显示。
我是否可以将UIAlertView的显示延迟到与其关联的viewController显示给用户的那一刻(调用viewDidAppear方法)?
答案 0 :(得分:2)
在视图控制器上声明要显示视图的属性。
@interface DownloadViewController : UIViewController
{
UIAlertView *downloadAlertView;
}
@property (retain) UIAlertView *downloadAlertView;
@end
然后,当您检测到错误时,请设置视图控制器的downloadAlertView属性(这将要求您通过正在下载的对象保留对此视图控制器的引用)。
- (void)downloadFailed
{
UIAlertView *alertView = [[[UIAlertView alloc] init] autorelease];
alertView.title = @"Download Failed";
downloadViewController.downloadAlertView = alertView;
}
然后在DownloadViewController
实施中,
- (UIAlertView *)downloadAlertView
{
return downloadAlertView;
}
- (void)setDownloadAlertView:(UIAlertView *)aDownloadAlertView
{
// standard setter
[aDownloadAlertView retain];
[downloadAlertView release];
downloadAlertView = aDownloadAlertView;
// show the alert view if this view controller is currently visible
if (viewController.isViewLoaded && viewController.view.window)
{
[downloadAlertView show];
downloadAlertView = nil;
}
}
- (void)viewDidAppear
{
if (downloadAlertView)
{
[downloadAlertView show];
downloadAlertView = nil;
}
}
快速解释: