Reputation: 1329
I want to segue from a tableview that is embedded in a navigation controller to another view controller once I clicked on a table cell. I still want that second view controller to be embedded in the same navigation controller. Hence, I created the segue via storyboard.
However, I want to conditionally check after tapping on the cell if the segue should be performed or not.
I already checked out the method shouldPerformSegueWithIdentifier
, which would satisfy my needs, but unfortunately, it is executed before the table click handler. If I remove the storyboard segue and just segue via performSegue
, my second view controller is not embedded in the same navigation controller anymore.
Any advice?
Here's my code:
// Called on click event on table cell
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let app = apps[indexPath.section]
if app.state == .INSTALLED {
let inputVC = self.navigationController?.viewControllers[1] as? InputVC
inputVC?.app = app
performSegue(withIdentifier: "showAccSync", sender: self)
} else {
// show alert
}
}
Upvotes: 2
Views: 4921
Reputation: 682
Programmatically, you can do like this
let lvc = storyboard?.instantiateViewController(withIdentifier: "yourViewController") as? yourViewController
self.navigationController?.pushViewController(lvc, animated: true)
Upvotes: 1
Reputation: 5689
Assign storyboard identifier to your InputVC
, Say "input_vc"
. Then instantiate it with storyboard. Then push to your InputVC
it would be on same navigation stack.
Add the code in didSelectRow
as:
Then instantiate your InputVC
with storyboard. Then push to your InputVC
it would be on same navigation stack.
guard let inputVC = storyboard?.instantiateViewController(withIdentifier: "input_vc") as? InputVC else { return }
inputVC.app = app
self.navigationController?.pushViewController(secondVc, animated: true)
Upvotes: 1
Reputation: 58129
You should start the storyboard segue from the view controller itself instead of a cell. That way, it won't execute automatically if you tap on a cell.
If you want to push the view controller to the navigation stack without using a storyboard segue, use UINavigationController
's pushViewController
:
if let secondVc = storyboard?.instantiateViewController(withIdentifier: "Second") {
navigationController?.pushViewController(secondVc, animated: true)
}
Upvotes: 8