UITableViewController init vs. initWithStyle

class ref says:

If you use the standard init method to initialize the UITableViewController object, a tabular view is created in the usual style.

I don’t understand where this behavior comes from - I would like to see me in some code or api, but

  • UITableViewController does not have init in api

  • how can the base class init know the appropriate default style for the derived class?

thanks for every hint

+4
source share
2 answers

Each object has an init method, but many classes have a so-called designated initializer. This is the primary initializer, and the rest are just convenience methods that invoke this designated initializer.

According to this document, in this case, the init method probably looks something like this:

 - (id)init { return [self initWithStyle:UITableViewStylePlain]; } 

The methods of the superclass are not displayed in the documentation of the derived class, except that the derived class overrides it and has something important to say about it. Therefore, you do not see init registered in the UITableViewController , it is the part of NSObject from which the UITableViewController is derived (via UIScrollView β†’ UIView β†’ UIResponder β†’ NSObject).

As for the second part of your question: the base class may (should) never know anything about derived classes. A derived class that wants to use a different default style simply overrides init again.

+8
source

in UITableViewController.m

 - (id) init { return [self initWithStyle:UITableViewStylePlain]; } 

The init method will call the designated initializer.

+2
source

Source: https://habr.com/ru/post/1343009/


All Articles