如何将表视图添加到导航控制器,导航控制器本身作为子视图控制器添加到UIViewController

时间:2014-01-10 19:32:20

标签: ios objective-c uinavigationcontroller childviewcontroller

我将UIViewController作为childViewController添加到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];

然后我将一个UINavigationController添加到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";

注意:我这样做是因为我需要导航控制器位于视图的中心(不占用整个屏幕尺寸)。

现在我想将TableView添加到此navigationController并继续正常导航。我该怎么做我无法将tableview添加到此导航控制器。

2 个答案:

答案 0 :(得分:0)

您传递给initWithRootViewController的参数错误。那应该是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";

答案 1 :(得分:0)

您无法将表格视图添加到导航控制器。您只能将包含表视图的视图控制器添加到导航控制器。