从子视图调用PushviewController不工作

问题描述:

我正在构建一个iPhone应用程序,具有以下结构: 我有MainViewController它由2个视图(如分屏)组成。 第一个他们认为,有一个按钮。上抽头,一个UITableView(ResultTableViewController)出现在视图(上面的):从子视图调用PushviewController不工作

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

在此先感谢!

好的,我找到了。 我不得不声明我的MainViewController为UINavigationControllerDelegate,并在其中创建一个辅助NavigationController。我在我的新的navigationController中推viewController,就是这样。

您正在将子控制器的视图添加到控制器的视图中,而不是将子控制器推到导航堆栈上。因此,您的孩子控制器的导航控制器将为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]; 
    } 
} 
+0

这似乎是正确的,但现在childView并没有显示出来。我想我的.xib有一些事情要做。在我的MainViewController.xib中,我有一个ResultsTableViewController,我将它与相应的IBOulet(结果)和视图连接起来。 – George 2012-02-14 10:54:30

+0

@Andy Riordan - 你可以展示UINavigationController的创建方式和位置?它是在XIB还是显式代码中? – mobibob 2013-05-14 02:35:48

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

上面的代码将推动navigationcontroller堆栈中的DetailView线。检查你的tableviewcontroller是否在那个堆栈上?