Swift 5, Xcode 12.4
I've got multiple UIViewController
s and when there's an error in the very last one (VC4), then I want to go back to the previous one (VC3) after displaying an error message.
VC3:
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
displayExtraInfo(indexPath.row)
}
private func displayExtraInfo(_ pos:Int) {
performSegue(withIdentifier: "segue3-4", sender: self)
}
VC4:
override func viewDidLoad() {
doStuff()
doMoreStuff()
}
private func doStuff() {
if someCondition {
//do stuff
} else {
//Error!
//Display error message, when "okay" button is clicked:
//TODO: Go back to VC3
}
}
The layout in storyboard:
NC1 - VC1 - NC2 - VC2 - VC3 - VC4
Clicking a specific navigation bar button in VC2 loads VC3 ("Show (e.g. Push)" segue created in storyboard/XIB). To get to VC4 you have to click on a UITableView
item in VC3. In VC3 & VC4 the "<" button in the navigation bar was added automatically and it works.
I tried both solutions suggested in e.g. this answer:
self.navigationController?.popViewController(animated: true)
self.dismiss(animated: true, completion: nil)
navigationController
isn't nil
but neither version does anything and doMoreStuff()
is still called.
How do I go back to VC3 in the above Swift function, while still respecting the stack?