Will a UIViewController subclass also affect my UITableViewControllers? - ios

I need to add a common method to all my view controllers no matter what type they are. I notice though that some of my viewcontrollers inherit from UIViewController and some from UITableViewControllers.
How do I write a subclass that can be used for both?

UITableViewController inherits from UIViewController, so if you want to be able to call your custom method from both, you can write a category on UIViewController and then call methods from that category in any subclass of either UIViewController or UITableViewController. Here's a link to Apple's docs on categories and extensions. http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/chapters/occategories.html

if you add a category to UIViewController, you will be able to call those methods on UITableViewController subclasses, as UITableViewController is a subclass of UIViewController

Related

UITableViewController inheriting from UIViewController

In order to fix bug I need to change a UIViewController into a UITableViewController. Only thing is that the current viewController is a subclass of a base UIViewController. How can I inherit behavior from base? Creating a whole other BaseTableViewController sounds very redundant.

Communicating between UIViewController and UITableViewDelegate

I have a UIViewController which contains a UITableView (amongst other views).
The UITableView could get its cells from one of two UITableDataSource, depending on some condition.
My UITableDataSource class also acts as my UITableViewDelegate.
When a cell is selected (tableView:didSelectRowAtIndexPath) I may want to perform an action on the UIViewController, such as performSegue or show an alert.
What would be the best way to do this?
Add a weak reference to the UIViewController inside each datasource/delegate class
Create a delegate per datasource/delegate class which calls functions inside my UIViewController
Your suggestion here!
I considered making my UIViewController the UITableViewDelegate but as the cells are different depending on the source I thought it would get messy.
Note: I say "best way" but really I am just interested in alternate approaches
The most common approach is to subclass UITableViewController and implement UITableViewDelegate and UITableDataSource there. You can return whichever cell you require in cellForRowAtIndexPathbased on any state in your controller class.

How to link Table dataSource and delegate outlets to UITableViewController?

I'm new to Swift/iOS development.
I have a UITableView in my storyboard that I want to populate with some data. In my attempt to do so, I created a class that inherits from UITableViewController. The implementation is not yet complete, but my understanding is that by inheriting from this class I can provide an IBOutlet to both dataSource and delegate.
When I try to drag the outlet into the source file, I don't get the insertion point that I got when I tested this before. See image below:
What do I have to do to define this class as the handler for the UITableView?
Set your viewController to inherit from UIViewController, not from UITableViewController (It seems like your IB is set up like that).
Do not forget to set your class to ZeroconfTableController on the interface builder.
Than, you will be able to set the delegate and datasource. NOTE: the delegate and the datasource will not create IBOutlets.
Assign the delegate and the dataSource the following way:
Also make sure, your viewController conforms to the protocols.
class ZeroconfTableController: UIViewController, UITableViewDataSource, UITableViewDelegate {
In your storyboard, select the UITableView and change this to the name of your UITableViewController subclass. You then do not need to do any linking stuff.
If you're using your own UITableView, inherit from UIViewController, not UITableViewController.
If however, you want to use a UITableViewController (which personally I don't use) in your storyboard, then do inherit from UITableViewController. In this case you won't have to wire up the UITableViewDataSource nor the UITableViewDelegate.

GIDSignIn with UISwitch

Google's docs say that if "the class that implements GIDSignInUIDelegate is not a subclass of UIViewController, implement the signInWillDispatch:error:, signIn:presentViewController:, and signIn:dismissViewController: methods of the GIDSignInUIDelegate protocol". I'm using a UITableViewController.
How do I use these methods with a UITableViewController (and with a UISwitch)? Do I implement those methods in my appDel or view controller? So much confusion!!!!!
Fortunately UITableViewController is itself a subclass of UIViewController so you don't need to do anything special, just set your UITableViewController as the delegate for Google Sign-in.

Overriding the parent of a class with a subclass

I am looking for an efficient way to override the parent class of a UITableViewController. The parent class of UITableViewController is UIViewController. I have a subclass of UIViewController which is called SpecialViewController. I would like to make a subclass of UITableViewController called SpecialTableViewController. How can I achieve this?
UIViewController -> SpecialViewController -> UITableViewController -> SpecialTableViewController
Or am I breaking some rules?
I don't think its possible at compiletime. But its possible at runtime. Take a look here: Dynamically change an object's superclass

Resources