简体   繁体   English

仅当当前segue的parentController为X时,才如何展开到rootViewController?

[英]How do I unwind to the rootViewController only if the current segue's parentController is X?

My App has a total of 4 ViewControllers in a NavigationViewController: A,B, C and D. From ViewController A it is possible to go to B and C. From both B and C it is possible to go to D. In other words: 我的应用程序在NavigationViewController中总共有4个ViewController:A,B,C和D。从ViewController A可以转到B和C。从B和C都可以转到D。换句话说:

A --> B --> D
A --> C --> D

What I want to do is, when the user presses back in D, I want to go to the rootViewController (A) only if the previous ViewController was B. If the previous ViewController was C, I want to go back to C. 我想做的是,当用户向后按D时,仅在以前的ViewController是B的情况下,我才想转到rootViewController(A)。如果以前的ViewController是C,则想回到C。

I'm fairly new to iOS and this is my first app so I'm not sure what the best way to do this is. 我刚接触iOS,这是我的第一个应用程序,因此我不确定实现此目的的最佳方法是什么。 Your help would be appreciated. 您的帮助将不胜感激。

You can set some property in viewControllerD - 'needToGoBackHome' 您可以在viewControllerD中设置一些属性-'needToGoBackHome'

In your viewControllerB: 在您的viewControllerB中:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    ViewControllerD *vcd =  (ViewControllerD *)segue.destinationViewController;
    vcd.needToGoBackHome = YES;
}

In your viewControllerD - override the back behavior of the navigation if need to: 在您的viewControllerD中-如果需要覆盖导航的后退行为:

- (void)viewDidLoad 
{
    [super viewDidLoad];

    if (self.needToGoBackHome) {
        UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Home" style:UIBarButtonItemStyleBordered target:self action:@selector(goBackToA:)];
        self.navigationItem.leftBarButtonItem=newBackButton;
    }
}

- (void)goBackToA:(id)sender
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

edit 编辑

Of course, if you have more than one segue from viewControllerB, you need to set the segue's identifier, and check if it is the right segue in the 'prepareForSegue' method 当然,如果您从viewControllerB中获得多个segue,则需要设置segue的标识符,并在“ prepareForSegue”方法中检查它是否正确

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

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