How to add a table view to a navigation controller which itself is added as a childviewcontroller to a UIViewController

StackOverflow https://stackoverflow.com//questions/21052870

Question

I have a UIViewController added as a childViewController to a UIViewController.

exampleView = [[UIViewController alloc] init];

//Setting the frame of the child UIViewController

CGRect frame = self.view.frame;
frame.origin.y = frame.origin.y + 83.0;
frame.size.height = 200.0;
exampleView.view.frame = frame;

[self addChildViewController:exampleView];
[self.view addSubview:exampleView.view];
[exampleView didMoveToParentViewController:self];

I then add a UINavigationController to the childViewController

//Adding navigation controller to the child view.

UINavigationController *nav = [[UINavigationController alloc]     initWithRootViewController:exampleView];
nav.navigationBar.translucent = NO;
[exampleView.view addSubview:nav.view];
exampleView.title = @"mynameasdasd";

NOTE: I do this because I need the navigation controller to be in the center of my view (not occupying the entire screen size).

Now I want to add a TableView to this navigationController and continue with normal navigation. How should I do this. I am unable to add a tableview to this navigation controller.

Was it helpful?

Solution

The parameter you are passing to initWithRootViewController is wrong. That should be the view of the UITableViewController

UITableViewController *tvc = [[UITableViewController alloc] init];
tvc.delegate = <Put your UITableViewDelegate here>
tvc.datasource = <Put your UITableViewDatasource here>
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:tvc];
nav.navigationBar.translucent = NO;
[exampleView.view addSubview:nav.view];
exampleView.title = @"mynameasdasd";

OTHER TIPS

You cannot add a table view to a navigation controller. You can only add a view controller containing a table view to a navigation controller.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top