我有一个视图控制器,它有一个带有两个按钮的UISegmentControl。我加载了这个View,当SegmentValue发生了变化时,我会在视图中添加一个视图控制器。
这是MainViewController
在课程中我定义了控制器
var tabNavigation: UISegmentedControl!
let viewController1:ViewController1 = ViewController1()
let viewController2:ViewController2 = ViewController2()
//创建分段控件
self.tabNavigation = UISegmentedControl(items: ["v1", "v2"])
self.tabNavigation.selectedSegmentIndex = 0
self.tabNavigation.frame = CGRectMake(60, 250, 130, 25)
self.tabNavigation.addTarget(self, action: "segmentedValueChanged", forControlEvents: .ValueChanged)
self.navigationItem.titleView = self.tabNavigation
// set first view
self.addChildViewController(viewController1)
self.view.addSubview(viewController1.view)
// Segement已更改
func segmentedValueChanged() {
if (self.tabNavigation.selectedSegmentIndex == 0) {
self.addChildViewController(viewController1)
self.view.addSubview(viewController1.view)
} else {
self.addChildViewController(viewController2)
self.view.addSubview(viewController2.view)
在故事板中,我有一个从MainViewController到ViewController3
的segue现在在ViewController1中,我正在尝试从ViewController1到ViewController3创建一个segue(应该是以前的导航控制器的一部分,然后用后退按钮滑动)。但由于ViewController1完全来自Code。
,因此不存在segue我的segue代码如下,但显然失败了,因为这是MainViewController到ViewController3的segue标识符
performSegueWithIdentifier("loadVC3", sender: nil)
答案 0 :(得分:0)
答案 1 :(得分:0)
您要找的是以编程方式初始化viewController
然后显示它,因为您首先需要设置一个唯一的Storyboard ID
,然后对其进行初始化。像这样:
let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("ViewController3") as! UIViewController
// and then present it modally
presentViewController(viewController, animated: true, completion: nil)
// or push it onto the navigation stack
pushViewController(viewController, animated: true)