Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Superclass for UITableViewController and UIViewController

Is any ViewController which is super class for UITableViewController and UIViewController ? Because I need it for not copy my business logic.

like image 681
Roman Barzyczak Avatar asked Aug 06 '26 08:08

Roman Barzyczak


2 Answers

As the other commenter said, UITableViewController is a fairly thin API on top of a UIViewController. You might be able to start from UIViewController and add the required logic to manage your table view to your custom class. However, there are some things that UITableViewController supports, like static table views and cell prototypes, that would be difficult or impossible to support if you did not use a UITableViewController.

Another alternative would be to create a category of UIViewController and add your extra methods to the category. Category methods are available to the class they are added to and all subclasses, almost as if you added the methods to the base class.

There are a couple of restrictions to category methods however.

  • You can't override methods from the base class, only add new methods.
  • You can't add instance variables in a category.
like image 81
Duncan C Avatar answered Aug 08 '26 23:08

Duncan C


i'm currently attempting to accomplish the same (create a superclass to place custom logic for all my VCs to execute). in my particular scenario, i want to track screen views using google analytics' API, which should be called in viewWillAppear. this would require me to place whatever code is required within that method for every view controller.

but i found this gem: method sizzling. thanks to mr. mattt thompson for that!

if you read closely, you will find that you will be able to create a category in which you have the power to replace the existing implementation of a given selector.

however as pointed out by @duncan d, if you seek to just extend behavior, you can simply create a regular category.

like image 42
Julian B. Avatar answered Aug 08 '26 23:08

Julian B.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!