Jake Boomgaarden Jake Boomgaarden - 1 month ago 16
Swift Question

Swift performSegueWithIdentifier shows black screen

I have read through a lot of different posts regarding this issue, but none of the solutions seemed to work for me.

I started a new app and I placed the initial ViewController inside a navigation controller. I created a second view and linked them together on the storyboard with a segue. The segue works successfully, and I can see the data I am transferring in a print statement from the second screen, but the screen shows black.

WelcomeScreen:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "segueToTraits"{
if let gender = self.selectedGender{
let traitVC = segue.destinationViewController as? TraitViewController
traitVC!.gender = gender
}
}
}

func sendGenderToTraitsView(gender : String?){
performSegueWithIdentifier("segueToTraits", sender: self)

}

@IBAction func button1(sender: UIButton) {
selectedGender = boyGender
self.sendGenderToTraitsView(selectedGender)
}


@IBAction func button2(sender: UIButton) {
selectedGender = girlGender
self.sendGenderToTraitsView(selectedGender)
}


Storyboard:
Link to image of my storyboard
My segue is set as follows:
Link to image of my segue information

Also, my viewControllers are named WelcomeViewController and TraitViewController. They have storyboard id's of welcomeVC and traitsVC.

Any help would be incredibly appreciated. Let me know if you need any other information.

Answer

I've build your app and everything works, maybe you've missed something, here is my solution (Note: Code is in Swift 3.0, but should be easy to adopt it to Swift 2.*):

The storyboard:

enter image description here

Set the segueToTraits identifier:

enter image description here

Set the TraitViewController class as custom class in the storyboard:

enter image description here

The view controller with the buttons:

import UIKit

class ViewController: UIViewController {  

  let boyGender = "boy"
  let girlGender = "girl"
  var selectedGender: String?


  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "segueToTraits"{
      if let gender = self.selectedGender {
        let traitVC = segue.destination as? TraitViewController
        traitVC!.gender = gender
      }
    }
  }

  func sendGenderToTraitsView(gender : String?){
    performSegue(withIdentifier: "segueToTraits", sender: self)

  }

  @IBAction func button1(sender: UIButton) {
    selectedGender = boyGender
    self.sendGenderToTraitsView(gender: selectedGender)
  }


  @IBAction func button2(sender: UIButton) {
    selectedGender = girlGender
    self.sendGenderToTraitsView(gender: selectedGender)
  }

}

The trait view controller:

import UIKit

class TraitViewController: UIViewController {


  var gender: String = ""

  override func viewDidLoad() {
    super.viewDidLoad()

    print("gender: \(gender)")
  }

}

Result:

enter image description here

Comments