从子视图调用时 PushviewController 不工作
我正在构建一个 iPhone 应用程序,其结构如下: 我有 MainViewController 它由 2 个视图组成(如分屏)。 它们的第一个视图有一个按钮。点击后,UItableView (ResultTableViewController) 出现在(上面的)第二个视图中:
-(IBAction)buttonTapped:(id)sender {
if ([(UIButton *)sender tag] == 0) {
ResultsTableViewController *childViewController = [[ResultsTableViewController alloc] init];
childViewController.tableView.delegate = self.results;
[self.results.view addSubview:childViewController.tableView];
}
}
所以我有一个 UItableView 作为 UIView 的子视图。
问题是 ResultTableViewController 的 didSelectRowAtIndexPath() 中的 PushViewController() 不起作用(self.navigationController 为零)。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailsViewController *detailView = [[DetailsViewController alloc] initWithNibName:@"DetailsViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:self.detailView animated:YES];
}
我尝试了许多找到的解决方案,但没有任何效果。
在我的 MainWindow.xib 中,我只添加了 MainViewController,这是问题所在吗?
提前致谢!
I'm building an iPhone app, with the following structure:
I have the MainViewController which consists of 2 views (like split screen).
The first view of them, has a button. On tap, a UItableView (ResultTableViewController) appears in the second (of the above) view:
-(IBAction)buttonTapped:(id)sender {
if ([(UIButton *)sender tag] == 0) {
ResultsTableViewController *childViewController = [[ResultsTableViewController alloc] init];
childViewController.tableView.delegate = self.results;
[self.results.view addSubview:childViewController.tableView];
}
}
So I have a UItableView as a sub-view of a UIView.
The problem is that pushViewController() in didSelectRowAtIndexPath() of ResultTableViewController does not work (self.navigationController is nil).
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailsViewController *detailView = [[DetailsViewController alloc] initWithNibName:@"DetailsViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:self.detailView animated:YES];
}
I have tried many of the solutions I found, but nothing works.
In my MainWindow.xib, I have only MainViewController added, is that the problem?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将子控制器的视图添加到控制器的视图中,而不是将子控制器推送到导航堆栈上。因此,您的子控制器的导航控制器将为
nil
,因为它没有放入导航控制器中。这就是你想要的吗?
You are adding the view of the child controller to your controller's view, not pushing the child controller onto your navigation stack. Due to that, your child controller's navigation controller will be
nil
, since it wasn't put into the navigation controller.Is this what you're going for?
上面的代码行将把详细视图推送到导航控制器堆栈上。检查您的 tableviewcontroller 是否在该堆栈上?
above line of code will push the detailview on navigationcontroller stack. Check whethere your tableviewcontroller is on that stack ?
好的,我找到了。
我必须将 MainViewController 声明为 UINavigationControllerDelegate 并在其中创建辅助 NavigationController。我将 viewController 推入新的 navigationController 中,就是这样。
Ok, I found it.
I had to declare my MainViewController as UINavigationControllerDelegate and create a secondary NavigationController in it. I push the viewController in my new navigationController and that's it.