使用等待对话框导航到另一个ViewController

时间:2012-10-22 21:36:19

标签: iphone objective-c ios

我正在尝试使用MBProgressHUD在我的应用中使用等待对话。

我的应用中有一个按钮。当用户点击它时,我们导航到另一个viewController:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ViewController2 *yourViewController = (ViewController2 *)[storyboard instantiateViewControllerWithIdentifier:@"FavoritePage"];
[self.navigationController pushViewController:yourViewController animated:YES];

如您所知,它也可以从interface builder设置。

事情是需要一段时间,直到下一页加载,我想在此期间显示等待对话:

- (IBAction)navigation:(id)sender {
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    ViewController2 *yourViewController = (ViewController2 *)[storyboard instantiateViewControllerWithIdentifier:@"FavoritePage"];
    [self.navigationController pushViewController:yourViewController animated:YES];
}

但它不起作用。

当我删除与导航相关的代码时,只需使用它:

[MBProgressHUD showHUDAddedTo:self.view animated:YES];

出现等待对话框,但是当我使用导航代码和此行时,不显示等待对话框并且导航有效。

如何让它们一起工作?

只想添加它来隐藏等待对话,我也有:

- (void) viewDidDisappear:(BOOL)animated {
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}

2 个答案:

答案 0 :(得分:1)

而不是,

- (IBAction)navigation:(id)sender {
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    ViewController2 *yourViewController = (ViewController2 *)[storyboard instantiateViewControllerWithIdentifier:@"FavoritePage"];
    [self.navigationController pushViewController:yourViewController animated:YES];
}

尝试,

- (IBAction)navigation:(id)sender {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    ViewController2 *yourViewController = (ViewController2 *)[storyboard instantiateViewControllerWithIdentifier:@"FavoritePage"];
    [self.navigationController pushViewController:yourViewController animated:YES];
    [MBProgressHUD showHUDAddedTo:self.navigationController.view  animated:YES];
}

或者,您需要在showHUDAddedTo的{​​{1}}方法中添加ViewController2方法。

答案 1 :(得分:0)

我认为问题出在您的- (IBAction)navigation:(id)sender {} ,因为您首先显示MBProgressHUD,然后推送到ViewController2。因此,MBProgressHUD已被展示,但随之而来的是它也推动了新观点。

如果要显示等待标志,则可以将MBProgressHUD与计时器一起使用。将计时器设置为1.0或2.0秒。之后调用navigation方法。使计时器失效并删除MBProgressHUD

否则,您可以使用

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(yourMethod) name:@"notificationName" object:nil];

在< - (IBAction)导航中

:( id)发送者{}。

并从NSNotification等其他视图中调用此[[NSNotificationCenter defaultCenter] postNotificationName:@"notificationName" object:nil]; `