I'd like to have a BaseViewController
class (that inherits from UIViewController
) for my project that can act as a base class for other view controllers in my project. This is to add things such as a common progress HUD, a refresh button & network updating mechanism, perhaps an error dialog that can be customized with text by each subclass, etc.
Crucially, I want to be able to add this functionality to both UIViewController
subclasses AND UITableViewController
subclasses, but I'm unable to insert myself into the UITableViewController
class hierarchy, so effectively I need a BaseUIViewController
and a BaseUITableViewController
, but if I do this I have to implement the changes twice and keep them in sync.
What's the best approach to have a single place for common code that has to be accessible both to BaseViewController
subclasses and to BaseTableViewController
subclasses? I only want one place in my code where I would have to deal with common tasks.
1) I've thought about using a category with associative references, but the problem here is that I need to have custom implementation of the ViewController lifecycle classes such as viewDidLoad:, and it's not a good idea.
2) I could skip UITableViewController
entirely, and build up my own BaseTableViewController
from a BaseViewController, implementing tableView
delegate
& datasource
methods myself, but I'm not so keen to skip apple's controller implementation here.
3) I could have a BaseViewController
inheriting from UIViewController
, BaseTableViewController
inheriting from UITableViewController
, and just put custom method calls into a third file ViewControllerBaseMethods
that can be invoked from both 'Base' files. There might still be duplication of properties, but at least the method code would be in one place.
Any other good approaches for this type of thing?
The nearest ancestor in the view controller hierarchy that is a tab bar controller.
First of all iOS is a system not a language. In Swift there is no universal base class : Swift classes do not inherit from a universal base class. Classes you define without specifying a superclass automatically become base classes for you to build upon.
This method is called after the view controller has loaded its view hierarchy into memory. This method is called regardless of whether the view hierarchy was loaded from a nib file or created programmatically in the loadView method.
To create a segue between view controllers in the same storyboard file, Control-click an appropriate element in the first view controller and drag to the target view controller. The starting point of a segue must be a view or object with a defined action, such as a control, bar button item, or gesture recognizer.
All UITableViewController does is provide a UITableView property, declare itself as the delegate and data source, and set its view as the table view
so create a BaseTableViewController which extends BaseViewController, declare it as a UITableViewDataSource and UITableViewDelegate and implement the required methods, add a UITableView property and implement loadView:
- (void)loadView
{
self.tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] andStyle:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.datasource = self;
self.view = self.tableView;
}
if you want to use different table view styles, you can provide a common method or property to take the table view style from when initialising
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With