iOS切换回后台UIViewController

时间:2014-02-26 19:01:32

标签: ios iphone objective-c uiviewcontroller storyboard

我正在寻找一种方法来呈现当前UIViewController的模态视图,基本上显示UIActivityIndi​​cator并强制用户在加载数据时等待。

在BaseViewController.m(我所有的UIViewControllers的基类)中:

// show loading view
-(void) showLoading
{
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
    LoadingViewController *loading = [storyBoard instantiateViewControllerWithIdentifier:@"loadingView"];

    loading.view.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0.7];
    self.modalPresentationStyle = UIModalPresentationCurrentContext;
    [self presentViewController:loading animated:NO completion:nil];
}

这很好用,但是如何在加载视图完成后返回后台视图?

需要stopLoading方法返回原始视图:

// stop loading
-(void) stopLoading
{
    // code here
}

如果我在呈现加载视图后尝试呈现新视图:

UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
UIViewController *view = [storyBoard instantiateViewControllerWithIdentifier:@"loadingView"];
[self presentViewController:view animated:YES completion:nil];

调试器提供警告:

Attempt to present PropertyPickerViewController: 0x8af6010 on ViewController: 0x8ab23c0 which is already presenting LoadingViewController: 0x8acf530

2 个答案:

答案 0 :(得分:2)

尝试:

[self dismissViewControllerAnimated:YES completion:nil];

事实上,我不确定用GIF动画呈现新的控制器是个好主意。

最佳选择是(imo)show UIActivityIndicator + place a view on top on all other views to prevent user from clicking anything

答案 1 :(得分:1)

您必须先[self dismissViewControllerAnimated:YES completion:nil]

检查Apple Documentation