Mahesh Babu Mahesh Babu - 6 months ago 15
iOS Question

iOS Navigation Back Button

I’m having a scenario, 

I had 3 controllers, lets assume A,B and C. When app launches I’m transitioning from A to C using segue. Later, in C by using a button I’m moving to B using segue again. So, now I’m in B when I click “Back” it is transitioning to C, but it has to move to A.



How is this possible, any hint/idea?

Answer

Put this code in B controller's back button.

for viewcontroller in self.navigationController!.viewControllers as Array {
            if viewcontroller.isKindOfClass(HomeVC) { // change HomeVC to your viewcontroller in which you want to back.  
                self.navigationController?.popToViewController(viewcontroller as! UIViewController, animated: true)
                break
            }
        }

OR If Class A is your RootView controller

self.navigationController?.popToRootViewControllerAnimated(true)
Comments