JamesG
JamesG

Reputation: 1601

Swift ViewController without prepareForSegue

On my project I have a Master controller (Which is accessed by a segue that has three nibs. These nibs are not on the main storyboard They only show on the master view and I can swipe across to view them.

How do I get from any one of those nibs to the ViewController that sent them?

For Clarity

I want to get from ViewController3 to ViewController1 but they are not on the storyboard so I cant click and drag and make a segue.

I hope this makes sense.

Edit: IBAction

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let secondVC = storyboard.instantiateViewControllerWithIdentifier("ViewController1StoryBoardID") as! ViewController1
presentViewController(secondVC, animated: true, completion: nil)

ViewController2 has this:

let vc3 = ViewController3(nibName: "ViewController3", bundle: nil) 
var frame3 = self.vc3.view.frame
frame3.origin.x = self.view.frame.size.width * 2
self.vc3.view.frame = frame3
self.addChildViewController(self.vc3)
self.scrollView.addSubview(self.vc3.view)
self.vc3.didMoveToParentViewController(self)

With a few extra lines to add other frames and slide to them

Upvotes: 0

Views: 264

Answers (4)

Lubos
Lubos

Reputation: 1209

You do it like:

let storyboard = UIStoryboard(name: "StoryboardName", bundle: nil)
let secondVC = storyboard.instantiateViewControllerWithIdentifier("anIdentifier") as! UIViewController
presentViewController(secondVC, animated: true, completion: nil)
  • "StoryboardName" is the name of your storyboard
  • "anIdentifier" is the identifier of your UIViewController set in storyboard

Please check the image where to set the storyboard ID of the ViewController:

enter image description here

Upvotes: 0

Ulysses
Ulysses

Reputation: 2317

There is a concept in iOS of Navigation View Controller that will help you to do it in a "professional way". You will "stack" the views, and can get back to any of them, white out a segue, just popping it.

Other way to do it, is moving by the identifier of the view, whithout an explicit segue in storyboard, check This link for more details

Upvotes: 0

fatihyildizhan
fatihyildizhan

Reputation: 8832

let viewController = YourViewController(nibName: "YourViewController", bundle: NSBundle.mainBundle())
self.navigationController?.pushViewController(viewController, animated: true)

Upvotes: 0

VictorG
VictorG

Reputation: 186

You can instanciate the Viewcontroller through the Storyboard,

//instanciate storyboard    
let mainStoryBoard = UIStoryboard(name:"StoryboardName", bundle:nil)

let myViewcontroller = mainStoryBoard.instanciateViewControllerWithIdentifier("viewcontrolleridentifier") as? ViewController1

You can set the identifier in Interface builder

Upvotes: 0

Related Questions