Almazini Almazini - 1 year ago 91
Swift Question

UIView is nil in prepareForSegue method

sorry for confusing title, I didn't know how to make it more meaningfull.

I was implementing information window using this example: github

I have added few labels into popView and linked them to secondaryVieController.

I have amended prepareForSegue method in PrimaryViewController to:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if segue.identifier == "showInfo" {

let info = segue.destinationViewController as! SecondaryViewController
info.adrLabel.text = branch.address
info.nameLabel.text =
info.telLabel.text =

dim(.In, alpha: dimLevel, speed: dimSpeed)

But when I run it i get error "fatal error: unexpectedly found nil while unwrapping an Optional value"

it is because info.popupView is nil.

But can you explain me how should I set it? And why it is working fine if there are no any additional labels.


Answer Source

The first method where you will be access this

info.adrLabel.text = branch.address
info.nameLabel.text =
info.telLabel.text =

values will be viewDidLoad() of SecondaryViewController.

You need to create variables and assign values in those variables and in the viewDidLoad() you can use those variables to assign the value to your labels.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download