I'm fairly new to iOS development and I'm building an application that consists of a UINavigationController and three ViewControllers that I'll refer to as A, B and C.
View controller A is shown initially. From there, I have two workflows:
A > C
A > B > C
In the second workflow, I have defined shouldPerformSegue(withIdentifier) on view controller B. Inside of this method, I need to do some work to decide if I should show view controller C or cancel the segue and display an error message. Assuming that no errors occurred, the segue to view controller C will be performed. When view controller C is displayed, the back button points back to view controller B but I need it to point back to view controller A instead.
I've tried removing view controller B from the navigation stack by calling the following at the end of shouldPerformSegue(withIdentifier) just before returning true to allow the segue to proceed:
self.navigationController?.viewControllers.remove(at: index)
If I read the documentation properly, the index of the view controller to remove corresponds to the following:
The root view controller is at index 0
The back view controller is at index n-2
The top view controller is at index n-1
Unfortunately, I haven't had any luck getting this to work properly. I'm sure that I'm doing something wrong but I'm not sure what else to try. Can anyone point me in the right direction? Thank you!