Alexander Alexander - 6 months ago 56
iOS Question

Performing Segue using code gives shows black screen

I am trying to do a segue to another screen using code but it shows me a black screen using Xcode 7 beta 6.
here is my 1st view controller file code

// ViewController.swift
// Segue through programming
//

import UIKit

class ViewController: UIViewController{

@IBAction func buttonPressed(sender: AnyObject) {
presentViewController(secondController(), animated: true) { () -> Void in

}
}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

Rob Rob
Answer

This will only work if secondController programmatically creates its view. If you want to use a storyboard scene (which is far more common), you can do the following:

@IBAction func buttonPressed(sender: AnyObject) {
    let controller = storyboard?.instantiateViewControllerWithIdentifier("foo")
    presentViewController(controller!, animated: true, completion: nil)
}

That obviously assumes that you've specified a storyboard identifier for the destination scene.

enter image description here

Or, you can created a segue between the two scenes in IB by control dragging from the view controller icon at the top of the first scene to the second scene:

enter image description here

and then give that segue its own storyboard id:

enter image description here

Then you can invoke the segue programmatically:

@IBAction func buttonPressed(sender: AnyObject) {
    performSegueWithIdentifier("bar", sender: self)
}