使用导航控制器在视图控制器之间进行隔离

时间:2014-03-11 18:26:09

标签: ios ios7 segue xcode5.0.1

假设我有一个带有2个按钮的ViewController A,每个按钮都与ViewController B和C相对应 (查看B的一个按钮和C的其他按钮)。从ViewController C到segConing到ViewController D. 所有ViewControllers都带有导航栏,因此从视图B i可以返回到A,并且从视图D i可以返回到D-> C-> A. 问题是当我在视图D到B之间进行切换时:执行了segue但是现在在B的导航栏中我重新调整以查看D并且我希望B将像它应该返回到A. 什么是解决方案????

1 个答案:

答案 0 :(得分:2)

据我所知,这可以通过两种方式完成。

1)使用D控制器上的自定义后退按钮

2)更改navigationController的堆栈,即D控制器类中的self.navigationController.viewControllers的NSArray

使用自定义后退按钮

你可以在D控制器中执行以下操作。仅当A控制器是navigationController的rootViewController时才有效。如果你跟踪你的A控制器,你也可以使用以下

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

    - (void)viewDidLoad
    {
    [super viewDidLoad];
     UIBarButtonItem *backButton = [[UIBarButtonItem alloc]  initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(goBack:)];
     self.navigationItem.leftBarButtonItem = backButton;
      self.navigationItem.hidesBackButton = YES;
    }
    
    -(void)goBack:(id)sender
    {
    [self.navigationController popToRootViewControllerAnimated:YES];
    

    }

更改navigationController的堆栈

如果您想要导航到特定的控制器,这很有用。但是从B回到A控制器后仍然会显示后退按钮,您可能需要通过进行一些更改来看到自己。不要尝试更改prepareForSegue中的导航堆栈

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSMutableArray *controllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
    NSMutableSet *controllersToRemove = [NSMutableSet new];
        for (id viewController in controllers) {
            if (![viewController isKindOfClass:[self class]]&&![viewController isKindOfClass:[NRViewController class]]) {
                    [controllersToRemove addObject:viewController];
                }
        }

        for (id controller in controllersToRemove) {
            [controllers removeObject:controller];
        }
    self.navigationController.viewControllers = controllers;
}

自定义后退按钮的编辑代码

如果您想要原始的后退按钮

你必须使用图像,代码如下

UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 60.0f, 30.0f)];
    UIImage *backImage = [[UIImage imageNamed:@"back_button_normal.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 12.0f, 0, 12.0f)];
    [backButton setBackgroundImage:backImage  forState:UIControlStateNormal];
    [backButton setTitle:@"Back" forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = backButtonItem;