UITableViewController inheriting from UIViewController - ios

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.

Related

Adopting multiple protocols with Objective-C iOS

So, I'm teaching myself iOS development and Objective-C. I completely new to Xcode and Objective-C but not programming.
I might be going about this completely wrong, but I'm trying call a method from a parent view controller from the child view controller. I tried to use delegation, but I ran into the problem of adopting multiple protocols of different types to the same class. Since I have a table view in my parent view controller, I'm unsure on how to adopt my child view controller delegation.
Essentially, I want to adopt both of these at the same time:
#interface MainViewController : NSObject <AddSiteViewControllerDelegate>
and
#interface MainViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
I'm new to the language, so this question might have a simple answer.
Since you want both at the same time, it has to be as below
#interface MainViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, AddSiteViewControllerDelegate >
UIViewController is a child of NSObject, so by inheriting UIViewController, you would get the behaviors defined in NSObject.
Objective-C supports single inheritance only. That is, a given class can derive from only one other class. However, a class can implement any number of protocols. So a simple solution to your problem would be:
#interface MainViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, AddSiteViewControllerDelegate>

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

How to create custom UIViewController then subclass already created UIViewControllers with it?

I have many classes subclasses of UIViewController. I would like to create another subclass of UIViewController and then make all my previous classes become subclasses of this one. Is that possible? If yes, how?
To clarify:
UIViewController > my previous UIViewControllers
now, what I would like:
UIViewController > new UIViewController > my previous UIViewControllers
Try looking up categories. They are an objective C way of extending a class. This way rather than having to go back and change your original code.

Set the subclass for `ECSlidingViewController`

I'm trying to add the ECSlidingViewController in my project, but I'm a novice on iOS and I'm not sure what to do in order to follow the instructions: "Add a UIViewController to your storyboards and set the subclass to ECSlidingViewController"
I've added the UIViewController, but now how do I set the subclass?
I don't use storyboards, but it sounds like you have to set custom class for this controller in identity inspector.
Suppose, you added an UIViewController in StoryBoard named "InitialSlidingViewController".
Then in InitialSlidingViewController.h, you should make the interface declaration as follows.
#interface InitialSlidingViewController : ECSlidingViewController
And please check the demo given by "ECSlidingViewController". You will understand it then.
I think you've got class and subclass the wrong way around.
What you are creating is a class called ECSlidingViewController that is a subclass of UIVIewController.
In you ECSlidingViewController.h you should have the following...
#interface ECSlidingViewController : UIViewController
This means you are defining a class called ECSlidingViewController and it is a subclass of UIViewController.

Will a UIViewController subclass also affect my UITableViewControllers?

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

Resources