尝试从UISegmentControl中的子视图启动UINavigationController

时间:2013-07-19 20:35:00

标签: ios uitableview uinavigationcontroller uisegmentedcontrol

我在一个ViewController中有UISegmentControl,我在其中嵌入了一个NavigationController用于导航。在那个UISegmentControl中,我有三个3段,我在三个UITableViewControllers之间切换。这些UITableViewControllers在从一个段切换到另一个段时显示在子视图中。我想要做的是当用户点击一个单元格时,我想在该视图中显示相同的DetailViewController。

以下是切换UISegmentControl的代码 -

-(IBAction)valueChanged:(id)sender{

 segmentControl=(UISegmentedControl*)sender;


 if (segmentControl.selectedSegmentIndex==1) {

 firstView = [self.storyboard   instantiateViewControllerWithIdentifier:@"FirstTopViewController"];

    [self.view addSubview:firstView.view];

    NSLog(@"1");
}
}

在FirstTopViewController.m中 -

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


DetailViewController *detailView = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];

FirstViewController *firstView = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstViewController"];

detailView.detail = [self.jobsArrayFromAFNetworking objectAtIndex:indexPath.row];

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:firstView];

[self.view addSubview:nav.view];

[nav pushViewController:jobDetailView animated:YES];

[tableView deselectRowAtIndexPath:indexPath animated:YES];

}

UINavigationController出现,但在与UITableViewController相同的子视图中,并在我尝试滚动或点击任何段时崩溃。如何正确实现导航?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

你差不多了,这就是我认为你需要做的事情:

valueChanged方法中,您可以设置UINavigationController及其根viewController(firstViewController)。像这样:

FirstViewController *firstView = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstViewController"];

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:firstView];

[self.view addSubview:nav.view];

然后在didSelectRowAtIndexPath做类似的事情:

DetailViewController *detailView = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];

[self.navigationController pushViewController:detailView]

有意义吗?

编辑:重点是,在开始时组合你的navController和firstViewController(在分段控件值改变后),然后在单元格选择后创建并将detailViewController推送到现有的navController。