Which initializer(s) to override for UITableViewController subclass

后端 未结 5 1069
滥情空心
滥情空心 2020-12-28 18:05

I have a UITableViewController subclass that\'s instantiated, depending on where it\'s used, in a NIB or via code. In both cases I want to do customization in

相关标签:
5条回答
  • 2020-12-28 18:40

    To clarify, initWithStyle:, being UITableViewController's only published initializer in the docs, is its one explicit designated initializer.

    initWithNibName:bundle: is inherited from UIViewController and is the designated initializer for that class. As such, in accordance with Cocoa guidelines, UITableViewController must override this method (by implementing it). However, this does not make it a designated initializer of UITableViewController.

    initWithCoder: is, as you point out, an implicit designated initializer from NSCoding.

    0 讨论(0)
  • 2020-12-28 18:42

    An addition to the posts above that reference –initWithCoder:

    If you added added the view controller to its parent via interface builder (for example: if the view controller is connected to a tab bar controller in interface builder), then you need to override –initWithCoder.

    (-initWithNibName will only be called when you create the view controller programmatically.)

    0 讨论(0)
  • 2020-12-28 18:52

    Implement:

    - (void) viewDidLoad
    

    and do your component initialization there.

    It has the advantage of only doing the initialization when the view is actually requested.

    Or just make a separate setup method invoked by all initializers.

    0 讨论(0)
  • 2020-12-28 18:56

    My confusion was based on the mistaken belief that each class should have a single designated initializer. This is not true, and in the case of UITableViewController there are 3 designated initializers (as far as I can tell):

    1. initWithStyle: declared locally
    2. initWithNibName:bundle: inherited from UIViewController
    3. initWithCoder: from adopting NSCoding protocol

    You need to override 1 or more of these in your subclass depending on how your subclass gets instantiated. In my case I had to implement #2 and #3 since the class can be loaded from a NIB, or instantiated via code with reference to a NIB. (I imagine it's rare that you'll use both initWithStyle: and initWithNibName:bundle: for a single class.)

    I found Apple's Coding Guidelines for Cocoa helpful.

    0 讨论(0)
  • 2020-12-28 18:56

    Internally,

    • UITableViewController's -initWithStyle: calls the super's -init then set the _tableViewStyle ivar.
    • UIViewController's -init simply calls -initWithNibName:bundle: with default arguments.
    • UITableViewController does not override -initWithNibName:bundle:.

    Therefore, if you override -initWithNibName:bundle: then -initWithStyle: will adopt the change too. Of course, to play safe (as you shouldn't rely on implementation details), override both of them.

    (And no need to override -initWithCoder: unless you will un/archive the instances.)

    0 讨论(0)
提交回复
热议问题