2011-04-20 55 views
2

我遇到了问题。我尝试使用基于导航的应用程序创建项目。
当我按rightBarButtonItem推到下一个视图。
并且该视图在UINavigationBar上获得了UISegmentedControl权限。
enter image description here关于UISegmentedControl和UINavigationController的问题

我用一个IBAction为当按下按钮:

-(IBAction)backButtonPressed:(id)sender{ 
[self.navigationController popViewControllerAnimated:YES];} 

当第一个视图展现出来,我按下按钮它会返回主视图。
如果我在UISegmentedControl上按下数字2,它会变成另一个视图,
仍然是相同的方法( - (IBAction)backButtonPressed:(id)sender)。
但是,当我按下按钮B,它不会再回到主视图..
enter image description here

如下是我对UISegmentedControl方法:

-(void)showSegmentedView:(id)sender{ 
AView *aView = [[AView alloc] initWithNibName:@"AView" bundle:nil]; 
BView *bView = [[BView alloc] initWithNibName:@"BView" bundle:nil]; 

if(seg.selectedSegmentIndex ==0) { 
    [[seg_view subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)]; 
    [seg_view addSubview:aView.view]; 
} 
else if(seg.selectedSegmentIndex ==1){ 
    [[seg_view subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)]; 
    [seg_view addSubview:bView.view]; 
} 

}

请问什么错?
提前感谢。

迷你

回答

3

我认为seg_view的的viewController已被推到navigationController的堆栈,self.navigationController上seg_view - 控制器然后返回你的navigationController。但是,从其他viewControllers AView/BView添加子视图时,这些UIViewControllers与seg_view的Controller或navigationController本身没有任何关联。这意味着新AView/BView中的self.navigationController是零!根据你的实现,backButtonPressed不会被调用,或者AView中的popViewController或BView不做任何事情,因为它们没有navigationController。我建议你要么不使用其他viewControllers(将2个视图放在与seg_view相同的nib中并将它们交换)或将它们推送到navigationController的堆栈上。