如何等待视图进入视图层次结构?

时间:2015-09-02 09:34:40

标签: ios objective-c uiviewcontroller

我的应用程序正在运行,直到我删除了一些发出网络请求的代码(因此这导致了几秒钟的等待)。我想这是足够的时间让视图加载到窗口层次结构......但是现在我删除了这个网络请求并且直接尝试打开该视图,我收到此消息:

警告:尝试显示其视图不在窗口层次结构中!

在尝试打开视图之前,我怎么能等到视图出现?

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
StoreFlyersViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"TabBar"];
[self presentViewController:vc animated:NO completion:nil];

2 个答案:

答案 0 :(得分:1)

如果我错了,请纠正我,但似乎你正在推动/呈现一个视图控制器(称之为A)和另一个(StoreFlyersViewController),例如来自viewDidLoad的{​​{1}}方法 - 当A的视图尚未出现在窗口层次结构中时。

要解决此问题,您可以将代码移至A。或者,如果这不能产生您想要的结果,您可以尝试将代码移动到单独的方法,并通过performSelector:withObject:afterDelay:以短暂的延迟(例如0.1秒)调用该方法。

答案 1 :(得分:-1)

试试这个;

dispatch_async(dispatch_get_main_queue(), ^{
       UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
StoreFlyersViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"TabBar"];
[self presentViewController:vc animated:NO completion:nil];
    });
相关问题