简体   繁体   English

从导航控制器弹出多个视图

[英]Popping multiple views from the navigation controller

I have an application which its rootview is a menu to 4 tableviews that the user uses to set up a search query by selecting a cell that loads another subview, so the basic structure looks like this 我有一个应用程序,其rootview是4个tableviews的菜单,用户通过选择加载另一个子视图的单元格来设置搜索查询,因此基本结构如下所示

Root View
- Parent View (search view)
--Sub View (user selects variables here to fill search parameters of the parent view

But one of the Parent View search parameters requiers another sub view to be pushed onto the navigation stack so it would look like 但是其中一个父视图搜索参数要求将另一个子视图推送到导航堆栈,这样看起来就像

Root View
- Parent View (search view)
--Sub View (user selects variables here to fill search parameters of the parent view
---Sub View (related values to the previous subview i.e. Model / sub model)

I would like to know if there is a way to pop back to the Parent View from this Sub View.. I know you can pop a single view or pop back to rootview but on this occasion I want to pop two subviews... is this possible? 我想知道是否有办法从这个子视图回弹到父视图..我知道你可以弹出一个视图或回弹到rootview但是在这种情况下我想要弹出两个子视图...是这可能吗?

UINavigationViewController UINavigationViewController

popToViewController:animated: popToViewController:动画:

Pops view controllers until the specified view controller is at the top of the navigation stack. 弹出视图控制器,直到指定的视图控制器位于导航堆栈的顶部。

- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated

You can add a category to UINavigationController to allow multiple controllers to be popped at once. 您可以向UINavigationController添加类别,以允许一次弹出多个控制器。

UINavigationController+VariablePop.h UINavigationController的+ VariablePop.h

#import <UIKit/UIKit.h>

@interface UINavigationController (VariablePop)

- (NSArray *)popViewControllers:(int)numPops animated:(BOOL)animated;

@end

UINavigationController+VariablePop.m #import "UINavigationController+VariablePop.h" UINavigationController + VariablePop.m #import“UINavigationController + VariablePop.h”

@implementation UINavigationController (VariablePop)

- (NSArray *)popViewControllers:(int)numPops animated:(BOOL)animated {
    NSMutableArray* returnedControllers = [NSMutableArray array];
    int indexToPopTo = self.viewControllers.count - numPops - 1;
    for(int i = indexToPopTo+1; i < self.viewControllers.count; i++) {
        UIViewController* controller = [self.viewControllers objectAtIndex:i];
        [returnedControllers addObject:controller];
    }
    UIViewController* controllerToPopTo = [self.viewControllers objectAtIndex:indexToPopTo];
    [self popToViewController:controllerToPopTo animated:YES];
    return returnedControllers;
}

@end

And then from the view controller you can: 然后从视图控制器中,您可以:

NSArray* poppedControllers = [self.navigationController popViewControllers:2 animated:YES];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM