使用导航控制器在故事板中呈现视图控制器 - Swift [英] Present View Controller in Storyboard with a Navigation Controller - Swift

查看:103
本文介绍了使用导航控制器在故事板中呈现视图控制器 - Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在我的新故事板中显示一个viewController:

I am currently showing a viewController in my new storyboard below:

var storyboard : UIStoryboard = UIStoryboard(name: AccountStoryboard, bundle: nil)
var vc : WelcomeViewController = storyboard.instantiateViewControllerWithIdentifier("WelcomeID") as WelcomeViewController
vc.teststring = "hello"        
self.presentViewController(vc, animated: true, completion: nil)

但是,这会使viewcontroller没有嵌入式导航控制器。
我尝试将WelcomeID更改为故事板中的导航控制器 - 但是没有成功。

However, this presents the viewcontroller without its embedded navigation controller. I tried changing "WelcomeID" to the navigation controller within the storyboard - however to no success.

我在Objective -C中使用了这个,但是不知道如何转换为swift:

I got this working in Objective -C, however don't know how to transform into swift:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"SetupStoryboard" bundle:nil];
UINavigationController *navigationController1 = [storyboard instantiateInitialViewController];
navigationController1.modalPresentationStyle = UIModalPresentationFormSheet;
navigationController1.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

WelcomeViewController *vc = (WelcomeViewController *)navigationController1.viewControllers[0];
vc.teststring = @"Hello";

[self presentViewController:navigationController1 animated:YES completion:nil];

你怎么能在swift中做到这一点?

How can you do this in swift?

推荐答案

你肯定是在正确的轨道上。不幸的是,当您通过其故事板ID引用视图控制器时,它将忽略它嵌入任何内容的事实。对于segue,当你转向嵌入的东西时,目标视图控制器将是嵌入控制器,而不是你通常感兴趣的控制器。无论如何,你应该能够以类似的方式修复问题。 Objective-C,所以这只是语法移植的练习。

You're definitely on the right track. Unfortunately when you reference a view controller by its storyboard ID it will ignore the fact it is embedded within anything. Same goes for segues when you segue to something embedded, the destination view controller will be the embedding controller, not the controller you're usually interested in. Anyhow, you should be able to fix the problem in a similar way you've done in Objective-C, so this is just an exercise in syntax porting.

编辑:现在用字符串定义故事板名称

let storyboard : UIStoryboard = UIStoryboard(name: "AccountStoryboard", bundle: nil)
let vc : WelcomeViewController = storyboard.instantiateViewControllerWithIdentifier("WelcomeID") as WelcomeViewController
vc.teststring = "hello"        

let navigationController = UINavigationController(rootViewController: vc)

self.presentViewController(navigationController, animated: true, completion: nil)

或者你可以给你的嵌入视图控制器一个ID并实例化它。

OR you can give your embedding view controller an ID and instantiate that instead.

这篇关于使用导航控制器在故事板中呈现视图控制器 - Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆