"super" 在 awakeWithContext、willActivate、didActivate 和 willDisappear 中的正确位置

correct position of "super" in awakeWithContext, willActivate, didActivate and willDisappear

我知道你要写

    override func awakeWithContext(context: AnyObject?) {
    super.awakeWithContext(context)
// code here, after the super call
}

在 willActivate、didActivate 和 willDisappear 中,我必须将代码放在哪里?在 super.willActivate(), super.etc 调用之上还是之下? 谢谢。

在样板代码中,super 总是放在代码之前。

    override func awakeWithContext(context: AnyObject?) {
      super.awakeWithContext(context)
      //implement any of your code here
    } 

这与您子 class 的 class 的所有覆盖调用一起使用。首先调用你的超级调用并传入参数(如果有),然后实现你的代码。如果您在函数中的其他地方调用代码,您可能会得到意想不到的结果或不正确的设置,但从技术上讲,您可以在代码之后调用它。最佳做法是在第一行调用它,但要求在函数的某处调用它。