I have a side menu in my iOS app with several entries. Once I click on one of them, I want to segue to the corresponding view controller via pushing them onto the navigation stack. This is currently done in the following way:
// Called on click event on table cell
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.tableView.deselectRow(at: indexPath, animated: true)
// navigate to the corresponding view controller
switch(indexPath.row){
case 0:
let launchScreenNC = self.storyboard?.instantiateViewController(withIdentifier: "LaunchScreenNC") as? UINavigationController
self.navigationController?.pushViewController((self.launchScreenNC?.viewControllers.first!)!, animated: true)
break
case 1:
let connectionNC = self.storyboard?.instantiateViewController(withIdentifier: "ConnectNC") as? UINavigationController
self.navigationController?.pushViewController((self.connectionNC?.viewControllers.first!)!, animated: true)
break
case 2:
let syncNC = self.storyboard?.instantiateViewController(withIdentifier: "SyncNC") as? UINavigationController
self.navigationController?.pushViewController((self.syncNC?.viewControllers.first!)!, animated: true)
break
default:
// nothing to do
break
}
}
It works fine for switching the first time to a new view controller. However, once initiated I want to keep those instances alive to keep a state of variables associated with each view controller.
What is the best way to do this?
I tried with
self.navigationController?.popToViewController((self.syncNC?.viewControllers.first!)!, animated: true)
or saving them as instance variables within the menu view controller, for example. But none worked so far.
I appreciate any advice.