返回两个或更多控制器的后退按钮

时间:2011-05-06 12:19:51

标签: iphone ios back-button

我知道覆盖后退按钮功能不被认为是一个好的用户设计。然而,我有一个过程,在某些时候回去将没有任何意义。相反,我希望用户返回两个或更多控制器

因此,在某些ViewControllers中,单击后退按钮应该会触发多个ViewControllers的弹出,而不仅仅是前面的那个。我尝试了子类化NavigationController并重写popViewController-Method:

- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
    if([[self.viewControllers lastObject] class] == [MyCOntroller class]){
        [super popViewControllerAnimated:NO]; // pop once

        return [super popViewControllerAnimated:animated]; // pop twice
    } else {
        return [super popViewControllerAnimated:animated];
    }
}

但是我遇到了问题,因为ViewController在前面,NavigationTopBar不再同步。有人遇到过同样的问题吗?

3 个答案:

答案 0 :(得分:4)

你尝试使用

吗?
popToViewController:animated:
  

弹出视图控制器,直到   指定的视图控制器在   导航堆栈的顶部。

也许你可以为这些视图控制器设置自定义后退按钮,然后尝试

- (IBAction)backButtonPressed
{
[yourNavigationcontroller popToViewController:viewController animated:YES];
}

答案 1 :(得分:0)

您应该添加左栏按钮。

UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(backButton_clicked)];
self.navigationItem.leftBarButtonItem = leftBarButton;
[leftBarButton release];

-(void)backButton_clicked {
    [self.navigationController popViewControllerAnimated:YES];
    [self.navigationController popViewControllerAnimated:YES];
    //or pop to special view controller
    //[self.navigationController popToViewController:specVC animated:YES];
}

答案 2 :(得分:0)

另一种方法是删除要从导航堆栈中跳过的视图控制器。在下面的示例中,您将返回2个视图控制器:

NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray: self.navigationController.viewControllers];
[allViewControllers removeObjectAtIndex:[allViewControllers count]-2]; // 2 means last but one
self.navigationController.viewControllers = allViewControllers;