0
votes

I have 3 view controllers: VC1 -> VC3 -> VC2. Transit from VC3 to VC2 I made by next code:

let storyboard = UIStoryboard(name: "VCs", bundle: nil)
let myVC2 = storyboard.instantiateViewController(withIdentifier: "VC2") as? VC2
myVC2?.name = "Test"
self.navigationController?.pushViewController(myVC2!, animated: true)

In viewDidLoad() of VC2 I delete VC3 from viewControllers stack and left only VC1:

var navigationVCs = self.navigationController?.viewControllers
navigationVCs!.remove(at: 1)
self.navigationController?.viewControllers = navigationVCs!

All work well. But I have one visual "Issue". When the view controller moves from VC4 to VC3, and VC3 view starts a load, NavigationBar back button of VC3 first of all show title of VC4, and only then switches to VC1 title. How I can fix this and force VC2 show back button title of VC1? Thanks.

1

1 Answers

0
votes

You can pass through the back button title of VC1 to VC2 in the VC3 this way:

if let navigationController = UIApplication.shared.keyWindow?.rootViewController as? UINavigationController {
            if let previousVC = navigationController.viewControllers.first {
                let backItem = UIBarButtonItem()
                let title = previousVC.navigationItem.backBarButtonItem?.title
                backItem.title = title
                navigationItem.backBarButtonItem = backItem // This will show in the next view controller being pushed
            }
        }

before you call

self.navigationController?.pushViewController(myVC2!, animated: true)