Almazini Almazini - 7 days ago 6
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 = branch.name
info.telLabel.text = branch.tel

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.

Thanks.

Answer

The first method where you will be access this

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

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.

Comments