2

I have a pretty complicated function generating a value in one view controller (lets call it firstVC). This value is used in firstVC but I would also like to utilize this value in other view controller at a later time (which we'll call thirdVC). I know I could set up a protocol to rerun the function, but this would just take extra time and I would rather just pass the value. No segue exists between firstVC and thirdVC and the time at which thirdVC will be opened and utilize the value is unknown and will be at some later time. (I can still get to thirdVC by seguing: firstVC -> secondVC -> thirdVC). I thought one could set it up as follows:

//In firstVC
var currentVal: String = "Hello"

@IBAction func button(_ sender: UIButton){
    let storyBoard = UIStoryboard(name: "Main", bundle: nil)
    let thirdVC: thirdVC = storyBoard.instantiateViewController(withIdentifier: "thirdVC")
    thirdVC.passedValue = currentVal
}

With passedValue being a variable living in thirdVC. This, however, does not pass any information as when I try to check the passedValue on the thirdVC's viewDidLoad() it is empty... I also try checking the value later via a button (to ensure that viewDidLoad() running before the info is passed isn't the issue) and it is still empty.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Runeaway3
  • 1,439
  • 1
  • 17
  • 43
  • Possible duplicate of [Passing Data between View Controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – NSNoob Jul 12 '17 at 11:12
  • 1
    Also see: [Detailed SE documentation on this subject](https://stackoverflow.com/documentation/ios/434/passing-data-between-view-controllers#t=201707121111354890919) for both Obj C and Swift (Also sorry for the wrong DV as dupes are good and you have shown your effort. I was hitting UV and somehow hit DV. Corrected now) – NSNoob Jul 12 '17 at 11:12
  • What about a delegate? – George Jul 14 '18 at 17:02

1 Answers1

1

You need to set the value when instatiating the vc, can be done by using didSet as seen below.

var currentVal: String = "Hello"

@IBAction func button(_ sender: UIButton){
    let storyBoard = UIStoryboard(name: "Main", bundle: nil)
    let thirdVC: thirdVC = storyBoard.instantiateViewController(withIdentifier: "thirdVC")
    storyBoard.instantiateViewController(withIdentifier: "thirdVC")
    thirdVC.passedValue = currentVal
    self.present(thirdVC, animated: true, completion: nil)
}

third vc ..

class thirdVc: UIViewController {

    var someValue: String? = nil

    // MARK: - Value to set when passed
    var passedValue: String! {
        didSet {
            self.someValue = passedValue
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        print(self.someValue) // "Hello"
    }

}
andromedainiative
  • 4,414
  • 6
  • 22
  • 34
  • I do not want to use `self.present` as I do not want to go to the `thirdVC` at the same time that the info is passed. Also, I tried using the code you answered with and it is still giving me a nil value in the `thirdVC` – Runeaway3 Jul 12 '17 at 12:39
  • Try ```let thirdVC: thirdVC = storyBoard.instantiateViewController(withIdentifier: "thirdVC") as! thirdVC ``` Could be you're not holding the actual view controller as reference and can't access it's properties. I don't really understand what you mean not wanting to go the view at the same time info is passed. Why would you pass data to a view if you're not using it? If you need to store a value over the lifecycle in an application write to user defaults, core data or realm. You're question was about passing data to a view without segue which my answer will do. – andromedainiative Jul 12 '17 at 13:01
  • You can't hold a value in a viewcontroller if you aren't holding it in memory. – andromedainiative Jul 12 '17 at 13:05
  • I would like to hold the information because the function to obtain it is complicated and slow (relatively speaking) and that function is run on the viewDidLoad of the `firstVC`. I want that information to be used later whenever the user gets to the `thirdVC` (without needing to rerun the function). You are saying I should utilize one of those three methods (defaults, CR, or realm) instead? – Runeaway3 Jul 12 '17 at 13:10
  • 1
    If you need to hold a value accessible throughout the app there is several different approaches. You could make the function you are performing accessible through out the app by assigning the func as a method to a class you share as a singleton. You could assign values to user defaults. Or something like this: class MyAccessibleData { static let sharedInstance = MyAccessibleData() var myValue: String? func myBigDataFunc () { self.myValue = "Hello" } } // in vc3 let helloString = MyAccessibleData.shared.myValue // Hello – andromedainiative Jul 12 '17 at 13:39