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

时间:2012-01-12 01:17:20

标签: iphone ios uitableview uiviewcontroller uinavigationcontroller

我有一个应用程序,其rootview是4个tableviews的菜单,用户通过选择加载另一个子视图的单元格来设置搜索查询,因此基本结构如下所示

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

但是其中一个父视图搜索参数要求将另一个子视图推送到导航堆栈,这样看起来像

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)

我想知道是否有办法从此子视图弹回父视图.. 我知道你可以弹出一个视图或弹回rootview但是在这种情况下我想弹出两个子视图......这可能吗?

2 个答案:

答案 0 :(得分:11)

UINavigationViewController

<强> popToViewController:动画:

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

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

答案 1 :(得分:2)

您可以向UINavigationController添加类别,以允许一次弹出多个控制器。

<强>的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”

@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

然后从视图控制器中你可以:

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