1

I have a sent Action, as follows:

    @IBAction func showSettings(sender: AnyObject) {
     let settingsPicker = SettingsViewController()
     settingsPicker.setDelegate(self)
     let navigationController = UINavigationController (rootViewController: settingsPicker)
     self.presentViewController(navigationController, animated: true, completion: nil)  
     }

The method creates a controller, sets a reference to the delegate, and creates a navigation controller.

All this works, however the widgets defined in the story board do not appear. The SettingsViewController should manage a ui which is defined in a story board. I presume becuase I create it programmatically none of the widgets appear. The SettingsViewController does not programmatically create widgets, the are declaratively defined in the story board.

If I link (in the storyboard) the two controllers with a segue, then the widgets appear, but my action is not being used.

How can I use my action and present the view controller / ui as defined in the storyboard?

dasPing
  • 337
  • 1
  • 2
  • 19

2 Answers2

2

When you create a segue between your UIViewControllers, you should define an identifier, eg: "settingsSegue".

In your code you can then perform that segue by calling the segue with the identifier:

@IBAction func showSetting(sender: AnyObject) {
    performSegueWithIdentifier("settingsSegue", sender: nil)
}

To set up the SettingsViewController you should implement the following:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){

    if let settingsController = segue.destinationViewController as? SettingsViewController {
        settingsController.delegate = self
    }
}

Interacting with Storyboard and Segues

Laffen
  • 2,753
  • 17
  • 29
1

If you want to invoke a segue through code, see Laffen's answer.

If you want to create a view controller that's defined in your storyboard and then display it programmatically, use instantiateViewControllerWithIdentifier to create a new instance of your view controller, then display it to the screen as desired (present it modally, push it onto your navigation stack, or whatever you want to do.)

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • In addition, the `instantiateViewControllerWithIdentifier` method is available via the storyboard member : `self.storyboard!.instantiateViewControllerWithIdentifier("settings")` – dasPing Feb 15 '16 at 16:00