ios - Objective-C:其 View 不在窗口层次结构中

标签 ios objective-c xcode

我有这段代码可以工作,所以一旦我按下按钮, Storyboard中的 View 就会发生变化:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ViewController *viewController = (ViewController *)[storyboard instantiateViewControllerWithIdentifier:@"view1"];
[self presentViewController:viewController animated:YES completion:nil];

这段代码有效,但是当我再次使用它来改回我原来的 View 时:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ViewController *viewController = (ViewController *)[storyboard instantiateViewControllerWithIdentifier:@"view2"];
[self presentViewController:viewController animated:YES completion:nil];

它不起作用,我收到此错误:

Warning: Attempt to present <UIViewController: 0x863ad30> on <UITabBarController: 0x86309b0> whose view is not in the window hierarchy!

标识已设置,但我不是 viewdidload 中的函数,所以我不知道如何解决这个问题。

有人可以帮忙吗?

最佳答案

试试这个,

第一种方法

-(IBAction)signin:(id)sender
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"viewController"];
    [[[[UIApplication sharedApplication] delegate] window] setRootViewController:vc];

}

第二种方法(返回上一个 View )

- (IBAction)signout:(id)sender
{

    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"parentViewController"];
    [[[[UIApplication sharedApplication] delegate] window] setRootViewController:vc];

}

关于ios - Objective-C:其 View 不在窗口层次结构中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17503743/

相关文章:

ios - 将 deviceToken 从 Objective C 复制到 JavaScript

ios - 转至 TabBar Controller

ios - 将信号从 iPhone 发送到微 Controller

ios - 完成模态视图后重新加载 UICollectionView

iphone - 将 AppDelegate 用作单例是一种不好的做法吗?

ios - 录制从 AVPlayer 流式传输的音频

ios - 当专辑信息可用时,按专辑对歌曲数组进行排序

ios - UISplitViewController:在 iPhone 6 Plus 上旋转时崩溃

objective-c - 我可以在完成预期的功能或工作后停止某些部分的代码执行吗?

ios - 获取存储在应用程序文档文件夹中的文件内容