简体   繁体   中英

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

  • ViewController1
    • segue to ViewController2
      • display slide nibs (ViewController3).

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

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

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

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

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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM