我正在寻找一种在当前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

最佳答案

尝试:

[self dismissViewControllerAnimated:YES completion:nil];


实际上,我不确定用动画gif呈现新控制器是否是个好主意。

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

10-07 19:51
查看更多