从子视图调用时,PushviewController无法正常工作

时间:2012-02-13 23:20:35

标签: iphone uitableview sdk pushviewcontroller

我正在构建一个iPhone应用程序,具有以下结构: 我有 MainViewController ,它包含2个视图(如分屏)。 它们的第一个视图有一个按钮。点按,UItableView( ResultTableViewController )出现在 second (上图)视图中:

-(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为nil)。

- (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,这是问题吗?

提前致谢!

3 个答案:

答案 0 :(得分:0)

您正在将子控制器的视图添加到控制器的视图中,而不是将子控制器推入导航堆栈。因此,您的子控制器的导航控制器将为nil,因为它未放入导航控制器。

这是你想要的吗?

-(IBAction)buttonTapped:(id)sender {
  if ([(UIButton *)sender tag] == 0) {
      ResultsTableViewController *childViewController = [[ResultsTableViewController alloc] init];
      childViewController.tableView.delegate = self.results;        
      [self.navigationController pushViewController:childViewController animated:YES];
    }
}

答案 1 :(得分:0)

[self.navigationController pushViewController:self.detailView animated:YES]; 

上面的代码行将推送navigationcontroller堆栈上的detailview。检查你的tableviewcontroller是否在那个堆栈上?

答案 2 :(得分:0)

好的,我找到了。 我必须将我的MainViewController声明为UINavigationControllerDelegate并在其中创建辅助NavigationController。我在我的新navigationController中推送viewController,就是这样。