EDIT
This answer is outdated. The correct way to add a view controller's view as a subview of another view controller is to implement a container view controller.
https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html
ORIGINAL ANSWER
Your pattern is a bit out of the ordinary. The most common way to do what you are describing is to initialize and present SecondViewController. If that's in fact what you want I highly recommend you peruse Apple's docs on how to create, customize, and present UIViewController
and UITableViewController
.
If you know all of that and really want to do something custom then read on.
Instead you are creating an instance of SecondViewController and rather than presenting it, you are adding it's view to the current view controllers view. If you know what you're trying to accomplish and this is in fact your desired result then go for it.
There is more than one way to configure this general pattern. I'll stick to the simplest way in this example.
1) MainViewController
(MVC) should retain the SecondViewController
(SVC) instance in a property as long as it is needed.
2) SVC is a subclass of UITableViewController
so by default, it is set as the dataSource
and delegate
for it's UITableView
. That means you need to implement the UITableViewDataSource
and UITableViewDelegate
methods in SVC
for your table to be populated with data. Assuming MVC
knows what data needs to go into the table, it should pass this to SVC
. The simplest way is to define a property on SVC
that can be set in MVC
at initialization time.
3) Assuming there is a way to dismiss the table after it has been presented, you'll want MVC
to do that. Basically, MVC
would remove SVC
's view from it's superview and then set the SVC
property to nil.
Here's some quick psuedo-code. I wrote the bare minimum as an example.
// MainViewController.h
//
#import "SecondViewController.h"
@interface MainViewController : UIViewController
@property (nonatomic, strong) SecondViewController *svc;
@end
// MainViewController.m
//
#import "MainViewController.h"
@implementation MainViewController
// init and configure views w/ init, loadView, viewDidLoad, etc
// present SecondViewController
- (void)presentSecondViewController:(id)sender {
self.svc = [[SecondViewController alloc] init];
// this example uses an array as the SVC data
self.svc.tableData = @[@"first", @"second", @"third", @"fourth"];
self.svc.view.frame = self.view.bounds;
[self.view addSubview:self.svc.view];
}
// dismiss SecondViewController
- (void)dismissSecondViewController:(id)sender {
if (self.svc) {
[self.svc.view removeFromSuperview];
self.svc = nil;
}
}
// SecondViewController.h
//
@interface SecondViewController : UITableViewController
@property (nonatomic, strong) NSArray *tableData;
@end
// SecondViewController.m
//
@implementation SecondViewController
// init and configure views w/ init, loadView, viewDidLoad, etc
// override tableData getter to create empty array if nil
- (NSArray *)tableData
{
if (!tableData) {
_tableData = @[];
}
return _tableData;
}
// override tableData setter to reload tableView
- (void)setTableData:(NSArray *)tableData
{
_tableData = tableData;
[self.tableView reloadData];
}
// implement UITableViewDelegate and UITableViewDataSource methods using
// the self.tableData array
UITableViewController
or how to set the cell's content for theUITableView
? – Steven