简体   繁体   中英

how to perform segue from root view controller to the last view controller in the navigation stack in swift?

在此处输入图片说明

I have 3 View controllers that embedded in the navigation controller stack. RedVC, BlueVC and YellowVC. In the each view controllers there are button that will trigger the segue

from RedVC, I want to navigate either to BlueVC or to YellowVC. All the segue from RedVC to BlueVC and to YellowVC are connected using push show segue, not present modally.

There is no problem if the route is like this : RedVC -> BlueVC -> YellowVC

but if I push show segue from redVC directly to YellowVC, the back button in the YellowVC will direct me back to RedVC

在此处输入图片说明

I want, if I segue from RedVC to YellowVC, I want the back button in the YellowVC will direct me to BlueVC, not RedVC.

how to achieve this?

If you are moving from RedVC to YellowVC so you could not come back to BlueVC after tapping back of yellowVC.

But this can be achieve in a way like:

Keep a flag variable called : shouldShowYellowDirectly in BlueVC and set its value true and from RedVC in prepareForSegueMethod.

In BlueVC do this:

override func viewDidLoad() {
        super.viewDidLoad()

   if shouldShowYellowDirectly{
    performSegue(withIdentifier: "showYellow", sender: self)
   }
}

It will show BlueVC for fraction of second.

If you are to move from RedVC to YellowVC directly (skipping BlueVC ), it will rule out the possibility of BlueVC getting initialised. Thereby, when you'll perform back navigation from YellowVC , you have to forcefully initialise BlueVC for achieving what you want.
Though this is not advisable as it won't follow the default VC life cycle. But if you are to do so, you need to explicitly take care of the behaviour. As an example - persisting data.

One solution is to add another non-animated segue between red and blue.

Uncheck the "Animates" checkbox in the property inspector.

在此处输入图片说明

To go to yellow, perform this segue first and pass a true flag that indicates whether we are going to yellow to blue. In blue, handle this flag. If the flag is true, perform the animated segue to yellow. If we are just going to blue, the animated segue to blue will be performed and the flag will be false .

Another solution is to do this programmatically without segues. Give your blue and yellow VCs some identifiers:

在此处输入图片说明

To go to yellow, instantiate your blue and yellow VCs:

let storyboard = UIStoryboard.main!
let blue = storyboard.instantiateViewController(withIdentifier: "Blue")
let yellow = storyboard.instantiateViewController(withIdentifier: "Yellow")

And then push blue unanimated, and then yellow animated:

    navigationController?.pushViewController(blue, animated: true)
    navigationController?.pushViewController(yellow, animated: false)

Yes you can insert blueVC in prepareForSegue method. You can follow below code

override func performSegueWithIdentifier(identifier: String?, sender: AnyObject?) {
         super.performSegueWithIdentifier(identifier, sender: sender);

         if identifier == "xyz" {
            var navArray : [AnyObject]! = self.navigationController!.viewControllers
            navArray.insert(blueVC, atIndex: navStackArray.count - 2)
            self.navigationController!.setViewControllers(navArray, animated:false)
         }
     }

In above, blueVC you need to crate that object and put there. Hope this help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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