我正在寻找一种方法来在我当前的 UIViewController 上呈现模态视图,以基本上显示 UIActivityIndicator 并强制用户在加载数据时等待。
在 BaseViewController.m(我所有 UIViewController 的基类)中:
// 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];
}
效果很好,但是加载 View 应该完成后我如何返回到背景 View ?
需要一个 stopLoading 方法返回到原始 View :
// stop loading
-(void) stopLoading
{
// code here
}
如果我在呈现加载 View 后尝试呈现一个新 View ,如下所示:
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
UIViewController *view = [storyBoard instantiateViewControllerWithIdentifier:@"loadingView"];
[self presentViewController:view animated:YES completion:nil];
调试器给出警告:
尝试在 ViewController: 0x8ab23c0 上呈现 PropertyPickerViewController: 0x8af6010,它已经在呈现 LoadingViewController: 0x8acf530
。
最佳答案
尝试:
[self dismissViewControllerAnimated:YES completion:nil];
事实上,我不确定用动画 gif 呈现新 Controller 是否是个好主意。
最好的选择是 (imo) show UIActivityIndicator + 在所有其他 View 之上放置一个 View 以防止用户单击任何内容
。
关于iOS 切换回后台 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22050744/