我有这段代码可以工作,所以一旦我按下按钮, 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/