0

I have a view controller which has a UISegmentControl with two buttons. I load this View and when the SegmentValue has changed i'll add a view controller into the view.

This is MainViewController

In the class I define the controllers

var tabNavigation: UISegmentedControl!
let viewController1:ViewController1 = ViewController1()
let viewController2:ViewController2 = ViewController2()

// Create the segment control

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 changed

func segmentedValueChanged() {
    if (self.tabNavigation.selectedSegmentIndex == 0) {
        self.addChildViewController(viewController1)
        self.view.addSubview(viewController1.view)
    } else {
        self.addChildViewController(viewController2)
        self.view.addSubview(viewController2.view)

In the storyboard i have a segue from MainViewController to ViewController3

Now in ViewController1 i'm trying to create a segue (should be part of a previous navigation controller so swipe across with a back button) from ViewController1 to ViewController3. But fails as the segue does not exist as ViewController1 is made completely from Code.

My code for the segue is below but obviously fails as that is the segue identifier for MainViewController to ViewController3

performSegueWithIdentifier("loadVC3", sender: nil)
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • I should also note I tried adding ViewController1 in the storyboard with just the segue to ViewController3 but still fails with "no segue with identifier" –  Aug 20 '15 at 10:32

2 Answers2

0

You cannot create segues programmatically. Look at this answer:

Creating a segue programmatically

I would probably use a XIB to design all the view controllers that you create programmatically.

Community
  • 1
  • 1
Eppilo
  • 763
  • 6
  • 19
0

What you're looking for is to initialize a viewController programmatically and then show it, for that you need first to set a unique Storyboard ID and then initialize it. Like this:

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)
Simon
  • 1,076
  • 7
  • 13