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

发布于 2024-11-05 04:51:42 字数 659 浏览 1 评论 0原文

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

因此在某些 ViewController 中,单击后退按钮应该触发多个 ViewController 的弹出,而不仅仅是前面的一个。我尝试了子类化 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];
    }
}

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

I know that overriding the back button functionality is not considered a good user design. However I have process, at which at some point going back would'nt make any sense. Instead I would like the user to go back two or more controller

So within certain ViewControllers, clicking the back-button should trigger the pop of several ViewControllers, not just the one in front. I tried around with subclassing the NavigationController and overriding the 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];
    }
}

However I get problems, where the NavigationTopBar is not in sync anymore withe the ViewController being in front. Anybody ran into the same issues?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

八巷 2024-11-12 04:51:43

您应该添加左栏按钮。

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];
}

You should add left bar button.

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

And

-(void)backButton_clicked {
    [self.navigationController popViewControllerAnimated:YES];
    [self.navigationController popViewControllerAnimated:YES];
    //or pop to special view controller
    //[self.navigationController popToViewController:specVC animated:YES];
}
走过海棠暮 2024-11-12 04:51:43

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

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

Another way is to delete the viewcontrollers you want to skip from the Navigation stack. In example below, you go back 2 view controllers:

NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray: self.navigationController.viewControllers];
[allViewControllers removeObjectAtIndex:[allViewControllers count]-2]; // 2 means last but one
self.navigationController.viewControllers = allViewControllers;
暖心男生 2024-11-12 04:51:42

你尝试过使用吗

popToViewController:animated:

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

也许你可以为此类视图控制器自定义后退按钮,然后尝试

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

Did u try using

popToViewController:animated:

Pops view controllers until the
specified view controller is at the
top of the navigation stack.

Maybe u can have custom back buttons for such view controllers and then try

- (IBAction)backButtonPressed
{
[yourNavigationcontroller popToViewController:viewController animated:YES];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文