ViewDidLoad / Unload messages to super?

前端 未结 4 1986
走了就别回头了
走了就别回头了 2021-01-19 15:44

I have noticed code from other sources where the author has not added the messages to super that are shown below. I usually add them both, but I was just curious as to what

4条回答
  •  深忆病人
    2021-01-19 16:18

    I would recommend always send UIViewController delegate messages to super (e.g. viewDidLoad, viewDidAppear). In some cases, it is unnecessary, if you are subclassing UIViewController directly for example.

    Some classes, e.g. UITableViewController, require subclasses to do that, as it is documented: "You may override loadView or any other superclass method, but if you do be sure to invoke the superclass implementation of the method, usually as the first method call."

    The call to super gives the super class a chance to handle the event (e.g. reload the table data, animate the de-selection of the button, etc).

    Needless to say, you don't need an explicit method whose sole job is calling super.

提交回复
热议问题