【发布时间】:2011-09-05 13:20:43
【问题描述】:
在我的程序视图中,我有一个按钮,用这个按钮我打开一个子视图;这个子视图是一个带有表格视图的视图。当我推送表格视图的一行时,我想转到另一个视图,所以我想用导航控制器来做这个;我该怎么做?
【问题讨论】:
标签: xcode ios uitableview uinavigationcontroller
在我的程序视图中,我有一个按钮,用这个按钮我打开一个子视图;这个子视图是一个带有表格视图的视图。当我推送表格视图的一行时,我想转到另一个视图,所以我想用导航控制器来做这个;我该怎么做?
【问题讨论】:
标签: xcode ios uitableview uinavigationcontroller
当您单击表格的某一行时,您的表格视图委托将收到 tableView:didSelectRowAtIndexPath: 消息。
您可以将代码放在那里创建 UINavigationController 并将您的新视图推送到它。
此示例代码(来自另一个 answer of mine on S.O.)展示了如何做到这一点:
UINavigationController* navigation = [[UINavigationController alloc] init];
iVkViewController *overviewViewController = [[iVkViewController alloc] init];
overviewViewController.title = @"First";
[navigation pushViewController:overviewViewController animated:NO];
这应该可以帮助您走上正轨。
附带说明:您可能会从一开始就考虑使用导航控制器,这会使您的 UI 更加“行为良好”,但这最终取决于您的应用需求。
【讨论】:
如果 UIViewController addSubView 是 UITableViewController 的一个视图,你想推入 tableView:didSelectRowAtIndexPath 的方法,你应该检查 self.navigationController 是否为 nil。如果它是 nil,你可能应该使用
[self.parentViewController.navigationController pushViewController:controller animated:YES];
如果self.parentViewController也是nil,有时候,你必须设置一个@property来指出UITableViewController中的parentViewController,比如:
@property (nonatomic, weak) UIViewController *parentVC;
在 UIViewController 中:
UITableViewController *tableViewVC = [[UITableViewController alloc] init];
tableViewVC.parentVC = self;
在 UITableViewController 中,-tableView:didSelectRowAtIndexPath:
[self.parentVC.navigationController pushViewController:controller animated:YES];
【讨论】: