viewDidLoad called before prepareForSegue finishes

前端 未结 5 631
梦毁少年i
梦毁少年i 2020-12-24 12:46

I was under the impression that viewDidLoad will be called AFTER prepareForSegue finishes. This is even how Hegarty teaches his Stanford class (as recently as Feb 2013).

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-24 13:18

    I had run into similar confusions in the past. The general rules of thumb that I learned are:

    1. prepareForSegue is called before destination VC's viewDidLoad
    2. viewDidLoad is called only after ALL outlets are loaded
    3. Do not attempt to reference any destination VC's outlets in source VC's prepareForSegue.

    Another lesson learned with regards to prepareForSegue is to avoid redundant processing. For example, if you already segue a tableView cell to a VC via storyboard, you could run into similar race condition if you attempt to process BOTH tableView:didSelectRowAtIndexPath and prepareForSegue. One can avoid such by either utilizing manual segue or forgo any processing at didSelectRowAtIndexPath.

提交回复
热议问题