ios 将新的视图控制器链接到故事板?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7978932/
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
Linking a new viewcontroller to Storyboard?
提问by Justin
There is probably a simple solution but I can't figure it out.
可能有一个简单的解决方案,但我无法弄清楚。
I am using storyboards for the interface.
我正在为界面使用故事板。
I start with a tab bar controller, but before the user is allowed to use the app the user has to authenticate himself trough a loginview which is modally pushed at the start.
我从标签栏控制器开始,但在允许用户使用该应用程序之前,用户必须通过在开始时模态推送的登录视图对自己进行身份验证。
I want to configure the loginview at the same storyboard, but I can't seam to figure out how to link the view controller at the storyboard and my code.
我想在同一个情节提要上配置登录视图,但我无法弄清楚如何将情节提要中的视图控制器和我的代码联系起来。
What I have done:
我做了什么:
- Create a new UIViewController subclass trough file > new > new file.
- Drag a new UIViewController in the story board
- Set the class in the custom class tab
- drags a UILabel for test purpose.
- run
- 通过文件 > 新建 > 新建文件创建一个新的 UIViewController 子类。
- 在故事板中拖出一个新的 UIViewController
- 在自定义类选项卡中设置类
- 拖动 UILabel 用于测试目的。
- 跑
No label...
无标签...
回答by Scott Sherwood
Pull on a new UIViewController that will act as the login view controller onto the MainStoryboard. In the attribute inspector change the identifier to LoginViewController (or something appropriate). Then add
拉上一个新的 UIViewController,它将作为 MainStoryboard 上的登录视图控制器。在属性检查器中,将标识符更改为 LoginViewController(或适当的东西)。然后加
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:vc animated:YES];
}
to the First view controller and the login screen will be loaded from your storyboard and presented.
到第一个视图控制器,登录屏幕将从您的故事板加载并呈现。
Hope this helps.
希望这可以帮助。
回答by OSXMonk
The answer by Scott Sherwood above is most correct answer I found after lot of searching. Though very slight change as per new SDK (6.1), presentModalViewController shows deprecated.
上面 Scott Sherwood 的答案是我经过大量搜索后找到的最正确的答案。尽管根据新的 SDK (6.1) 进行了很小的更改,但presentModalViewController 显示已弃用。
Here is very small change to above answer.
这是对上述答案的很小改动。
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
HomeViewController * hvc = [sb instantiateViewControllerWithIdentifier:@"LoginView"];
[hvc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:hvc animated:YES completion:nil];
回答by Soul
I'm new in this field. But if the first view controller is a navigation view controller and its rootviewcontroller is a table view controller. If you want to push a view controller like the LoginViewController when you click the cell, and you also want to go back to the table view by using the navigation bar. I recommend this way:
我是这个领域的新手。但是如果第一个视图控制器是一个导航视图控制器并且它的根视图控制器是一个表视图控制器。如果您想在单击单元格时推送像 LoginViewController 这样的视图控制器,并且您还想使用导航栏返回到表格视图。我推荐这种方式:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *controller = [sb instantiateViewControllerWithIdentifier:@"LoginViewController"];
[self.navigationController pushViewController:controller animated:YES];
}
In this way, you can have the navigation.
这样,您就可以进行导航了。
By the way, I don't know why this kind of problem you asked will appear. I guess when the loginviewcontroller is created in the code, its view is not the view in the storyboard. If someone know the cause, please tell me! thanks!
对了,不知道为什么会出现你问的这种问题。我猜在代码中创建 loginviewcontroller 时,它的视图不是故事板中的视图。如果有人知道原因,请告诉我!谢谢!