mike vorisis
mike vorisis

Reputation: 2832

Updating outlets after dismissing a viewController

First of all let me explain what exactly is the scenario I want to archive.

My rootviewcontroller -> FirstViewController -> DetailViewController -> Detail2ViewController and then back to FirstViewController with a label updated by a function inside FirstViewController.

(Assume that -> means a press of a button)

Now I use this to call the function from the last viewController:

var go = FirstViewController()

self.go.get() //get is the function that updates the textfield text.

self.presentingViewController?.presentingViewController?.dismissViewControllerAnimated(true, completion: nil) 

Now I print the string I wanted to be the new text of my label and it works but when I use this line:

self.label.text = newone // newone is the new string 

I have a crash with this log:

fatal error: unexpectedly found nil while unwrapping an Optional value

Of course the same function is working when I first reach the FirstViewController and I also tried to use a string to insert to the label text like this:

self.label.text = "hello"

Thanks in advance.

EDIT:

I forgot to put navigation controllers.

NavContr -> rootviewcontroller -> FirstViewController -> DetailViewController -> NavContr ->Detail2ViewController

Upvotes: 0

Views: 58

Answers (1)

Joe
Joe

Reputation: 4074

var go = FirstViewController(). Everytime this will create a new instance of FirstViewController() and will not use your already initialized instance of viewController. Reportedly the label will be nil hence you are getting fatal error. Try by getting your already initialized object from stack(if you are using it and there is one).

Upvotes: 1

Related Questions