iPhone:向tableview子类添加导航栏

时间:2011-12-16 15:05:31

标签: iphone cocoa-touch uitableview uinavigationcontroller uinavigationbar

我的视图控制器将打开作为UITableView子类的模态视图。 UITableView子类没有导航栏。那么如何以编程方式添加它呢?因为我需要取消按钮来关闭这个模态视图。

2 个答案:

答案 0 :(得分:2)

将表视图控制器添加到导航控制器,并以模态方式显示导航控制器。例如(这是所有伪代码,因为我不确定确切的方法名称):

MyTableViewController *tvc = [[MyTableViewController alloc] init];
tvc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" target:self action:@selector(dismissModalViewController)];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:tvc]];
[self presentModalViewController:navController];

答案 1 :(得分:0)

如果您希望它使用导航栏和后退按钮推动视图并且您正在使用TableView控制器,那么为什么不使用NavigationController来推送它,因为这将默认为您提供导航栏和后退按钮?