5

I'm passing data between two different UIViewControllers located within two different .storyboard files within Xcode. (It's quite a large project so I had to break it up into different storyboards.)

I have already established an "exercisePassed" variable (String) within the view I would like to Pass my data to, and I already know how to move between two views located within different storyboards. Like so.

var storyboard = UIStoryboard(name: "IDEInterface", bundle: nil)
var controller = storyboard.instantiateViewControllerWithIdentifier("IDENavController") as! UIViewController

//**************************************
var ex = //Stuck Here
ex.exercisedPassed = "Ex1"
//**************************************

self.presentViewController(controller, animated: true, completion: nil)

How can I pass the data without using a Segue/PrepareForSegue?

Gavin Friel
  • 191
  • 1
  • 3
  • 13

4 Answers4

24

I'm assuming that the viewController that you want to pass the data to is a custom viewController. In that case, use this amended code here:

var storyboard = UIStoryboard(name: "IDEInterface", bundle: nil)
var controller = storyboard.instantiateViewControllerWithIdentifier("IDENavController") as! MyCustomViewController

controller.exercisedPassed = "Ex1"

self.presentViewController(controller, animated: true, completion: nil)
Swinny89
  • 7,273
  • 3
  • 32
  • 52
  • My view actually has an imbedded UINavigationController. Xcode just gave me the following error **Could not cast value of type 'UINavigationController' (0x2cdff8c) to 'myApp.myCustomViewController' (0x1d3e18).** any advice? Thanks btw. – Gavin Friel Aug 17 '15 at 13:44
  • Yes, try changing MyCustomViewController to UINavigationController – Swinny89 Aug 17 '15 at 13:47
  • I don't suppose you know how to similarly pass a variable between a .xib popover, and the screen it's currently displayed upon? Having some difficulties with it. Essentially - I want to pass a String from the popover, back to its parent screen. Do I need to reload the parent screen in some way? – Gavin Friel Aug 19 '15 at 12:49
  • 1
    How to do this in Swift 3 ? – mythicalcoder Nov 09 '16 at 07:00
3

your question is not very clear, as far as i have understood

//1
//create a variable in "IDENavController"
//e.g 
var someVariable = @""

//2
var controller = storyboard.instantiateViewControllerWithIdentifier("IDENavController") as!       UIViewController
controller.someVariable = [assign your variable]

//3
self.presentViewController(controller, animated: true, completion: nil)
Usama
  • 1,945
  • 1
  • 13
  • 20
2

Try this. This worked for me.

var storyboard = UIStoryboard(name: "IDEInterface", bundle: nil)
var controller = storyboard.instantiateViewControllerWithIdentifier("IDENavController") as! YourDestinationViewControllername
// Add your destination view controller name and Identifier

// For example consider that there is an variable xyz in your destination View Controller and you are passing "ABC" values from current viewController.

controller.xyz = "ABC"

self.presentViewController(controller, animated: true, completion: nil)

Hope this will be helpful.

Karlos
  • 1,653
  • 18
  • 36
1

For those who are looking for the Swift 3/4 answer:

let storyboard = UIStoryboard(name: "IDEInterface", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "IDENavController") as! CustomViewController

controller.yourVariable = "abc"

self.present(controller, animated: true, completion: nil)
Xcoder
  • 1,433
  • 3
  • 17
  • 37