ctm
ctm

Reputation: 88

How to make UIViewControllerSubclass a subclass of UITableViewController

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

Upvotes: 0

Views: 764

Answers (3)

Mike
Mike

Reputation: 9835

UITableViewController is a subclass of UIViewController, so when you create a new UITableViewController, it will be a subclass of both UITableViewController and UIViewController.

enter image description here

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:

enter image description here

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.

enter image description here

Upvotes: 0

rmaddy
rmaddy

Reputation: 318814

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.

Upvotes: 2

Joe Barbour
Joe Barbour

Reputation: 842

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 :)

Upvotes: 0

Related Questions