How to make UIViewControllerSubclass a subclass of UITableViewController - ios

I'm going through an Xcode tutorial and it's asking me to create a UIViewControllersubclass file and then make it a subclass of UITableViewController.
I'm using Xcode 5.1.1 and I still cannot find the UIViewControllersubclass template in creating a new file.
Someone please tell me how to do this in the latest version of Xcode.
Here is the tutorial for reference https://www.youtube.com/watch?v=2p8Gctq62oU (21:15)
Thanks in Advance

In Xcode go to the File | New menu. Choose File. Under iOS choose Source then Cocoa Touch class. Then give the new class a name and choose UITableViewController for the subclass.

UITableViewController is a subclass of UIViewController, so when you create a new UITableViewController, it will be a subclass of both UITableViewController and UIViewController.
To create a subclass of UITableViewController in Xcode, either go to File -> New File, or hit command+n, select Cocoa Touch under iOS, and select the Objective-C class:
You will then be prompted with a window like below. Name your new class and select UITableViewController in the subclass drop-down box. This "NewTableViewController" will be a subclass of UITableViewController, which is already a subclass of UIViewController as described above.

I think your getting a little confused with what a subclass is. A UIViewController is not a subclass, subclass would be a UIView class or an NSObject, something you import into a view controller to handle different tasks.
You can import a UIViewController into another UIViewController or in your case a UITableView but then it becomes a child not a subclass.
To import a subclass you call the subclass using
MySubclassFile *subclass = [[MySubclassFile alloc] init];
then you can call a method from that subclass like so...
[subclass mySubclassMethod];
import a UIViewController as a child is a little more complex but still
MyViewController *childView = [self.storyboard instantiateViewControllerWithIdentifier:#"STORYBOARD_IDENTIFYER"];
childView.view.frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.view.bounds.size.height);
[self addChildViewController:childView];
[self.view addSubview:childView.view];
I hope that helps :)

Related

What superclass for a custom UI component?

I need to create a custom UI component that will satisfy the following:
it will contain a composition of UIViews, Labels, Button and a TableView.
it should be possible to instantiate it programatically - or - placing a UIView in nib editor and change the class in the inspector to my custom component class. (and it would the maintain/keep the main frame values from placed UIView)
I already started experimenting with it, I chose the UIView but it doesn't handle the ..cellForRow... cell method. On the other hand, if I use a UIViewController, then my class change in the inspector doesn't work and the app crashes.
Which one should I choose - UIView, UIviewController or NSObject?
I would go for UIViewController or UITableViewController, since you said you are having a tableView in it. If you do a UIViewController, you would probably have to include another UITableViewController inside it, using addChildViewController, as explained here
I believe the reason why your app is crashing is that you you changed your class superclass from UIView to UIViewController, but in the Interface Builder you still just have a UIView object. You need to change it to a UIViewController in the Interface Builder, and select your class as the File Owner.
If you intend this to be a stand-alone component, with all of the logic contained within the control, then I would base it on a UIViewController, like Apple does with the MFMailComposeViewController. If you want it to be strictly a view, with no built-in logic of its own, but instead delegates that logic, then use a UIView and delegate the table view methods to the controller that is using it.

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.

Can't find NewViewController subclass in Storyboard view

I want to change a TableViewController to a new UIViewController (PlayersViewController) from a UIViewController subclass, so I added two files (.h, .m), and then went back to the Identity Inspector/Custom Class.
Expected to find the PlayersViewController in the pulldown menu in Custom Class but could not find it, so I typed it in and hit "Return", but that didn't work either.
Am I doing something wrong?
Your new class should inherit from UITableViewController, not UIViewController. Only subclasses of the base class are available to select in the custom class field.

IB - add UIView to UITableViewController?

I like to accomplish an UIToolbar below an UITableView and I wanted to use the UITableViewController because it sets up the data source and delegate and other stuff on its own.
I don't need an UINavigationController as has been proposed in similar topics due to only having 1 view currently and my data is without a hierarchy.
I didn't manage to drag and drop an UIView or UIToolbar to my UITableViewController at any place (scene design area or view hierarchy) in Interface Builder of XCode 4.2.
Thus my question: How to add an UIView to an UITableViewController in Interface Builder?
I did manage to achieve the look I intend to accomplish using an UIViewController with an UITableView and an UIToolbar contained in its UIView.
Would using an UIViewController with an UITableView be much more involved than relying on the UITableViewController?
Thank you for your time!
I think this is your real question
Would using an UIViewController with an UITableView be much more
involved than relying on the UITableViewController?
The answer is no, its not much more work. Just add this to the viewcontrollers' .h
#interface MyViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
Then in the .h add the datasource and delegate functions( you could just copy and paste the functions your'e currently using in your TableViewController)
NOTE: From Xcode 4.5.1: "Static table views are only valid when embedded in UITableViewController instances."

Display a UIView without a UIViewController

I'm trying to create a custom UIView that can be displayed at any point in the game. In other words, the UIView is not tied to a specific UIViewController.
Right now my solution is to store a UIViewController* in my custom UIView, set it whenever the UIViewController I'm using changes, and then adding the UIView as a subview of the controller.
I figure there has to be a better, and safer, way of handling this, but I haven't been able to find anything so far.
Thanks
Make a UIView subclass. File > New File > Objective-C Class > (subclass of ) UIView > Give it a Class Name.
So now you should have something like MyCustomView.h and .m
In the View Controller that you want to display the custom view import the header file
#import "MyCustomView.h"
Then simply add it to your current view
MyCustomView *myCView = [[MyCustomView alloc] initWithFrame:<Where You Want It>];
[self.view addSubview:myCView];
[myCView release];
You can use Interface Builder to graphically lay out the custom view. Or you can do it programmatically. If you're going the IB way, create a new file, click on User Interface, and View. Give it the same Class Name i.e. (MyCustomView).
Select the File Owner show the Identity Inspector (option command 3).
For Class, change it from NSObject to MyCustomView.

Resources