Is init?(aDecoder:) or prepareForSegue(_:sender:) called first?

时间:2016-07-11 22:15:04

标签: ios swift initialization uikit

Intro
I am trying to debug my program and I have come across a problem. I am getting the swift error: Unexpectedly found nil while unwrapping an optional value. This problem is from an implicitly unwrapped optional. The error is in ViewControllerB's init?(aDecoder:) and the place where I set the value is in ViewControllerA's prepareForSegue(_:sender:) that uses a "show segue" to go to ViewControllerB.

Problem
This leads me to beleive that the init?(aDecoder:) method in ViewControllerB is called before the prepareForSegue(_:sender:) in ViewControllerA. Am I right?

1 个答案:

答案 0 :(得分:3)

Yes. An object is always initialised before it can be used elsewhere in the application.

If there is work you need to do that depends upon the property being set, it's often appropriate to trigger that work from a didSet observer on the property or in the viewDidLoad() method.