ios InstantiateViewControllerWithIdentifier - Storyboard id 设置但仍然无法正常工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17088057/
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
instantiateViewControllerWithIdentifier - Storyboard id set but still not working
提问by norman_h
I have to created a segue programmatically however when I click on the button the view controller does not change to the next, Storyboard ID is set and still not working. Am I missing any other checks in order to make this work?
我必须以编程方式创建一个 segue 但是当我单击按钮时,视图控制器不会更改为下一个,故事板 ID 已设置但仍然无法正常工作。我是否错过了任何其他检查才能使这项工作正常进行?
Please see code below:
请看下面的代码:
EntryViewController *entryController = [self.storyboard instantiateViewControllerWithIdentifier:@"go"];
[self.navigationController pushViewController:entryController animated:YES];
its driving me mad. Thanks
它让我发疯。谢谢
回答by norman_h
Inside my viewDidLoad I have placed the button which calls goldStarOpen:
在我的 viewDidLoad 中,我放置了调用 goldStarOpen 的按钮:
UIButton *btnTwo = [UIButton buttonWithType:UIButtonTypeCustom];
btnTwo.frame = CGRectMake(250, 20, 40, 40);
[btnTwo addTarget:self action:@selector(goldStarOpen) forControlEvents:UIControlEventTouchUpInside];
[btnTwo setImage:[UIImage imageNamed:@"GoldStar.png"] forState:UIControlStateNormal];
[self.view addSubview:btnTwo];
Inside goldStarOpen I have code which is almost identical to yours.
在 goldStarOpen 中,我的代码几乎与您的相同。
- (void)goldStarOpen
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
@"MainStoryboard" bundle:[NSBundle mainBundle]];
UIViewController *myController = [storyboard instantiateViewControllerWithIdentifier:@"GoldStar"];
[self presentViewController:myController animated:YES completion:nil];
}
goldStarOpen activates a ViewController in the storyboard.
goldStarOpen 在情节提要中激活一个 ViewController。
You may need to set the Storyboard ID of the View Controller you are trying to load. This is located in the inspector, just below where you assign a custom class to your view controller.
您可能需要设置您尝试加载的视图控制器的 Storyboard ID。它位于检查器中,就在您为视图控制器分配自定义类的下方。
回答by Lithu T.V
Use
用
+ (UIStoryboard *)storyboardWithName:(NSString *)name bundle:(NSBundle *)storyboardBundleOrNil.
So try this
所以试试这个
self.storyboard=[UIStoryboard storyboardWithName:@"Your_Story_Board_Name" bundle:[NSBundle mainBundle]];
EntryViewController *entryController = [self.storyboard instantiateViewControllerWithIdentifier:@"go"];
[self.navigationController pushViewController:entryController animated:YES];
回答by Lê Tr??ng Giang
try this Storyboard Choose UIViewController-> Xcode->Editor->Emabed In->Navigation Controller
试试这个 Storyboard 选择 UIViewController-> Xcode->Editor-> Emabed In->Navigation Controller
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"YourStoryboard"
bundle: nil];
YourViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier: @"YourViewController"];
[self.navigationController pushViewController:vc animated:YES];
回答by Kalpesh
Try this,
尝试这个,
UIStoryboard * storyboardobj=[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
EntryViewController *entryController = [storyboardobj instantiateViewControllerWithIdentifier:@"go"];
[self.navigationController pushViewController:entryController animated:YES];
回答by Andy
Ran into this issue today, clearly set the Storyboard ID on the right panel but I was still getting the warnings that the VC was unreachable and did not have an identifier set. Fixed the issue after I unchecked the box that read "Inherit Module From Target" and ran the project.
今天遇到这个问题,在右侧面板上清楚地设置了 Storyboard ID,但我仍然收到警告说 VC 无法访问并且没有设置标识符。在我取消选中读取“从目标继承模块”并运行项目的框后修复了该问题。
After the code successfully ran, it seems to now work with or without the box checked so it might be an Xcode bug... Anyway hope this helps.
代码成功运行后,它现在似乎可以在选中或不选中该框的情况下工作,因此它可能是 Xcode 错误...无论如何希望这会有所帮助。
回答by Wain
You can't set the view controllers storyboard
. It's read only and set when the view controller is created. Instead, you will need to get the storyboard from somewhere else or load it.
您不能设置视图控制器storyboard
。它是只读的,并在创建视图控制器时设置。相反,您需要从其他地方获取故事板或加载它。
You could get the storyboard from the AppDelegate's root view controller. Or use + (UIStoryboard *)storyboardWithName:(NSString *)name bundle:(NSBundle *)storyboardBundleOrNil
.
您可以从 AppDelegate 的根视图控制器获取故事板。或使用+ (UIStoryboard *)storyboardWithName:(NSString *)name bundle:(NSBundle *)storyboardBundleOrNil
.