1

Error transferring value from one view controller to another on dismiss.

I have a value in ViewController2 that I would like to transfer to ViewController1 on dismiss. In view ViewController2 I do the following to dismiss:

func OtherFunction() {
    passAndDismiss(scannedScript: stringCodeValue)
}

Note: stringCode value is indeed being passed as a String to scannedScript

func passAndDismiss(scannedScript: String) {
  dismiss(animated: true, completion: {  
      let viewcontroller = ViewController1()
      viewcontroller.Textfield1.text = scannedScript
  })
}

The error occurs on line:

viewcontroller.Textfield1.text = scannedScript

The error I get is:

Unexpectedly found nil while implicitly unwrapping an Optional value

vadian
  • 274,689
  • 30
  • 353
  • 361
John
  • 965
  • 8
  • 16
  • `Textfield1` is nil. I guess it's an IBoutlet. It's either because it didn't load yet, or because it's in a UIStoryboard or a xib not called `ViewController1.xib`, and doing `ViewController1()` doesn't load it with the xib/storyboard, so doesn't load it with the IBOutlets... – Larme Sep 24 '19 at 16:51
  • Possible duplicate of [Passing values from one view controller to another in Swift](https://stackoverflow.com/questions/26676687/passing-values-from-one-view-controller-to-another-in-swift) – timbre timbre Sep 24 '19 at 16:53
  • best way is to use one of the known methods, shown in linked SO, or in many tutorials available onliine: https://learnappmaking.com/pass-data-between-view-controllers-swift-how-to/ – timbre timbre Sep 24 '19 at 16:53

2 Answers2

3

Two fatal issues:

  1. ViewController1() is a new instance of the controller which is not the instance in the storyboard.

    Solution: You need the real instance either with a segue or with instantiation from the storyboard.

  2. Even if ViewController1() was the expected controller the outlets are not connected right after the initialization.

    Solution: You have to declare a temporary property for the string in ViewController1 and assign the value to the label in viewDidLoad

Community
  • 1
  • 1
vadian
  • 274,689
  • 30
  • 353
  • 361
  • I believe this is what Mitesh also advised, but for some reason I get an error saying the textfield is not a member to the view controller, is there something else you would recommend? – John Sep 24 '19 at 18:20
  • Is there something I am noticeably doing different? – John Sep 24 '19 at 20:06
0

Here you are initialising viewcontroller with wrong way. You should load from xib or storyboard.

In your way not initialise xib or storyboards’ ui element objects. Here textfield object nil and you are trying to access text property of that. That’s why app crashed. You should use following way:

let myViewController = MyViewController(nibName: "MyViewController", bundle: nil)

OR

let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "someViewController")

If you are not using xib or storyboard then please initialise textfield object in that controller’s init method

Mitesh
  • 504
  • 2
  • 8
  • How should I access the textfield inside this view controller? I am doing `controller.Textfield1.text = scannedScript` but it is saying the TextField1 is not a member – John Sep 24 '19 at 17:48
  • You should able to access the textfield, not need to do anything. Just make sure not declare textfield as private. If still facing issue then share declaration code for textField – Mitesh Sep 24 '19 at 17:51
  • Originally the TextField is declared like this: `@IBOutlet weak var TextField1: UITextField!` and in the second view controller I do: `controller.Textfield1.text = scannedScript` – John Sep 24 '19 at 18:05
  • You should able to access this. Can you share the both controller files or sample code to check the issue. – Mitesh Sep 24 '19 at 18:12