Édouard
Édouard

Reputation: 13

performSegueWithIdentifier doesn't work in a different ViewController

I have the following storyboard: Main Storyboard

In it, several custom View Controllers are programmatically embedded in the Scroll View. In one of them, a button is present and should trigger a segue to show the "hey" screen.

I have then wrote the following code:

@IBAction func addNewDateButtonDidTouched(sender :AnyObject) {
    let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let storyboardInit = mainStoryboard.instantiateViewControllerWithIdentifier("mainview")
    storyboardInit.performSegueWithIdentifier("showNewDate", sender: self)
}

This @IBAction seems to reload the inital view controller and perform the segue correclty (Xcode doesn't return any error). But the "hey" screen doesn't show up and its viewDidLoad() doesn't load.

Any hint?

Upvotes: 0

Views: 962

Answers (3)

André Slotta
André Slotta

Reputation: 14030

if i understand your viewcontroller hierarchy correctly...

since the viewcontroller that contains the button to trigger the segue is a childviewcontroller of the viewcontroller that has the segue setup in storyboard i think you have to call presentingViewController?.performSegueWithIdentifier("showNewDate", sender: self).

Upvotes: 0

Nelley Chung
Nelley Chung

Reputation: 59

The storyboardInit in your code will instantiate a UIViewController that has an identifier "mainview". But this UIViewController hasn't been added to the screen, when you're asking the controller to perform a segue.

So, I wouldn't do that. What I would do, to segue to the hey screen is either :

  1. Create a segue from the current controller and just do this in the code : self.performSegueWithIdentifier
  2. Instantiate the hey controller like this : instantiateViewControllerWithIdentifier, and then just do the self.presentViewController

So, to transition to a new controller, you have to start from the current controller.

Upvotes: 0

Amloelxer
Amloelxer

Reputation: 772

Instead of calling

storyboardInit.performSegueWithIdentifier("showNewDate", sender: self)

try

self.performSegueWithIdentifier("showNewDate", sender: self)

Upvotes: 2

Related Questions