xcode Objective-C:其视图不在窗口层次结构中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17503743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Objective-C: whose view is not in the window hierarchy
提问by user2536879
I have this code that works so that once i press a button the view changes in my storyboard:
我有这段代码可以工作,所以一旦我按下按钮,我的故事板中的视图就会发生变化:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ViewController *viewController = (ViewController *)[storyboard instantiateViewControllerWithIdentifier:@"view1"];
[self presentViewController:viewController animated:YES completion:nil];
This code work but when i use it again to change back to my original view using:
此代码有效,但是当我再次使用它以使用以下方法更改回原始视图时:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ViewController *viewController = (ViewController *)[storyboard instantiateViewControllerWithIdentifier:@"view2"];
[self presentViewController:viewController animated:YES completion:nil];
It does not work and i receive this error:
它不起作用,我收到此错误:
Warning: Attempt to present <UIViewController: 0x863ad30> on <UITabBarController: 0x86309b0> whose view is not in the window hierarchy!
The identifies are set and i am not the functions in my viewdidload
so i do not know how to fix this.
标识已设置,我不是我的功能,viewdidload
所以我不知道如何解决这个问题。
Can anybody help?
有人可以帮忙吗?
回答by aToz
Try this,
尝试这个,
First Method
第一种方法
-(IBAction)signin:(id)sender
{
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"viewController"];
[[[[UIApplication sharedApplication] delegate] window] setRootViewController:vc];
}
Second method (to return back to the previous view)
第二种方法(返回上一个视图)
- (IBAction)signout:(id)sender
{
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"parentViewController"];
[[[[UIApplication sharedApplication] delegate] window] setRootViewController:vc];
}
回答by Tarek Hallak
This might happen if you call the method before calling makeKeyAndVisible
, so move the calling after that.
如果您在调用之前调用方法,则可能会发生这种情况makeKeyAndVisible
,因此请在调用之后移动调用。
If not try the hack below:
如果没有尝试下面的黑客:
[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:viewController animated:YES completion:nil];
回答by Ramy Al Zuhouri
The error message says all: you have to add the view controller's view to the window before presenting the view controller:
错误消息说明了一切:在呈现视图控制器之前,您必须将视图控制器的视图添加到窗口中:
UIWindow* keyWindow= [[UIApplication sharedApplication] keyWindow];
[keyWindow addSubview: viewController.view];
[self presentViewController:viewController animated:YES completion:nil];