Tarvo Mäesepp Tarvo Mäesepp - 27 days ago 5
Swift Question

Passing value gives nil even if value does exist

I tried passing value between viewControllers. But the problem is, the value is always nil.

In firstVC I do:

// Create a custom view controller
let ratingVC = RatingViewController(nibName: "RatingView", bundle: nil)

// Create the dialog
let popup = PopupDialog(viewController: ratingVC, buttonAlignment: .horizontal, transitionStyle: .bounceDown, gestureDismissal: true)

ratingVC.selectedTestString = "HELLO"


// Present dialog
self.present(popup, animated: true, completion: nil)


And in the secondVC:

var selectedTestString: String! //Unwrapping because I know value does exist

override func viewDidLoad() {
super.viewDidLoad()
print(selectedTestString) //Gives nil

}


I am doing it first time with
.xib
but I think the concept should be the same.

Answer

Set value of selectedTestString before creating the dialog, that will resolve the problem.

// Create a custom view controller
let ratingVC = RatingViewController(nibName: "RatingView", bundle: nil)

ratingVC.selectedTestString = "HELLO"

// Create the dialog
let popup = PopupDialog(viewController: ratingVC, buttonAlignment: .horizontal, transitionStyle: .bounceDown, gestureDismissal: true)

// Present dialog
self.present(popup, animated: true, completion: nil)
Comments