Iphone 返回两个或多个控制器的Backbutton

Iphone 返回两个或多个控制器的Backbutton,iphone,ios,back-button,Iphone,Ios,Back Button,我知道覆盖后退按钮功能被认为不是一个好的用户设计。然而我有一个过程,在这个过程中,在某一点上回到过去是没有任何意义的。相反,我希望用户返回两个或更多控制器 因此,在某些ViewController中,单击“后退”按钮应触发多个ViewController的弹出,而不仅仅是前面的一个。我尝试过将NavigationController子类化并重写popViewController方法: - (UIViewController *)popViewControllerAnimated:(BOOL)an

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

因此,在某些ViewController中,单击“后退”按钮应触发多个ViewController的弹出,而不仅仅是前面的一个。我尝试过将NavigationController子类化并重写popViewController方法:

- (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];
    }
}
但是我遇到了一些问题,NavigationTopBar不再与前面的ViewController同步。有人遇到过同样的问题吗?

你试过使用

popToViewController:animated:
弹出视图控制器,直到 指定的视图控制器位于 导航堆栈的顶部

也许你可以为这样的视图控制器定制后退按钮,然后再试一次

- (IBAction)backButtonPressed
{
[yourNavigationcontroller popToViewController:viewController animated:YES];
}
你有没有试过使用

popToViewController:animated:
弹出视图控制器,直到 指定的视图控制器位于 导航堆栈的顶部

也许你可以为这样的视图控制器定制后退按钮,然后再试一次

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

您应该添加左栏按钮

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


您应该添加左栏按钮

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


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

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

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

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

navigationItem具有属性“backBarButtonItem”。如果使用此按钮,则该按钮看起来像普通箭头。您将创建一个“普通”UIBarButtonim,它看起来与另一个完全不同。navigationItem有一个属性“BackBarButtonim”。如果使用此按钮,则该按钮看起来像普通箭头。你会创造一个“正常”的uibarbuttoneim,它看起来与另一个完全不同。