-4

Here is my code so far

joinButton.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
}
 @objc func buttonAction(_sender: UIButton) {
    print("I want to go to a storyboard here")
 }

I have everything coded (never used storyboard), but now I want to actually manually play with the UI now. How can I manually play with the storyboard? Or even render out to a different viewcontroller? Thanks!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
zyne
  • 25
  • 4

2 Answers2

0

You need to assign a Storyboard ID to the view controllers you are going to use programatically:

  1. Select storyboard
  2. Go to Identity Inspector (⌥⌘3)
  3. Assign Storyboard ID

If you want to add manual segue in Storyboard (Note: you can have multiple manual segues):

  1. Select storyboard
  2. Select the source view controller
  3. Go to Connections Inspector (⌥⌘6)
  4. In the Triggered Segues, drag the dot beside manual to the target view controller and select the desired method
  5. In Document Outline, select "Show segue..." and go to Attributes Inspector (⌥⌘4) and assign an Identifier

In your button function, you can simply use the segue by:

self.performSegue(withIdentifier: "<IdentifierForSegue>", sender: self)

Or if you do not wish to use a manual segue:

// If view controllers are in the same Storyboard
let storyboard = self.storyboard!
// If view controllers are in different Storyboards
// let storyboard = UIStoryboard.init(name: "<NameOfYourStoryboard>", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "<IdentifierForTargetViewController>")
// If you don't have a navigation controller
self.present(viewController, animated: true, completion: nil)
// If you have a navigation controller
// self.navigationController?.pushViewController(viewController, animated: true)
Bill
  • 469
  • 2
  • 4
  • I can't seem to get this to drag to my view controller. Keep in mind this is my only storyboard and that everything else was coded in. Do I make the segue going to itself or something? I don't get it :( – zyne Feb 26 '19 at 02:34
  • Say you have two view controllers A and B, if you want to go from A to B, then the select the storyboard and select the view controller A and to go `Connections Inspector`, you should see the `Triggered Segues` at top, there should be a dot beside `manual`, drag it to view controller B. – Bill Feb 26 '19 at 02:46
  • I only have one view controller since I created a collectionviewcontroller programmatically, so I can't even connect A to B. – zyne Feb 26 '19 at 03:13
  • The just follow the last step and either call `present` or `pushViewController` with the collectionviewcontroller you created – Bill Feb 26 '19 at 03:17
0

You can do it programatically this way:

Swift 3+

   let storyboard = UIStoryboard(name: "StoryboardName", bundle: nil)
   let vc = storyboard.instantiateViewController(withIdentifier: "ViewControllerID") as UIViewController
   present(vc, animated: true, completion: nil)