Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewController init vs. initWithStyle

the class ref says:

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

i do not understand, where this behaviour comes from - i would like to see i in some code or api but

  • UITableViewController has no init in its api

  • how could some base class' init know about a suitable default style for a derived class?

thanks for every hint

like image 243
Klaus Ahrens Avatar asked Dec 29 '22 00:12

Klaus Ahrens


2 Answers

Every object has an init method, but a lot of classes have a so-called designated initializer. That is the main initializer, and the others are merely convenience methods calling that designated initializer.

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

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

Methods from a superclass do not appear in the documentation of the derived class, except if the derived class overrides it and has something important to say about it. That's why you don't see init documented in UITableViewController, it's part of NSObject from which UITableViewController derives (through UIScrollView -> UIView -> UIResponder -> NSObject).

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

like image 51
DarkDust Avatar answered Jan 30 '23 09:01

DarkDust


in UITableViewController.m

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

The init method will call the designated initializer.

like image 42
Konrad77 Avatar answered Jan 30 '23 10:01

Konrad77