Perform a segue programmatically
Segues are components of storyboard. If you don't have a storyboard, you can't perform segues. But you can use present view controller like this:
let vc = ViewController() //your view controller
self.present(vc, animated: true, completion: nil)
Additional to the correct answer, please be aware that if you are in a navigation stack (say, you are in a Settings page and want to segue to an MyAccountVC) you should use:
let vc = MyAccountVC()
self.navigationController?.pushViewController(vc, animated: true)
If the navigated VC appears with a transparent background during the transition, just set the backgoundColor
of it to .white
.
In my case I was presenting a TableViewController, without a segue defined, and found it was necessary to instantiate from the storyboard otherwise tableViewCells
couldn't be dequeued in cellForRowAt
.
let vc = (storyboard.instantiateViewController(withIdentifier: "YourTableViewController") as? YourTableViewController)!
self.navigationController!.pushViewController( vc, animated: true)
To trigger a segue programmatically, you have to follow steps:
1. Set it up in Storyboard by dragging your desired segue between two view controllers and set its identifier (for example, in your case is "popOutNoteExpanded") in Attributes Inspector section.
2. Call it programmatically
performSegue(withIdentifier: "popOutNoteExpanded", sender: cell)
Please check if you set its identifier correctly or not.
Besides, in your above code, you put an incorrect sender.
In your prepare() method, you use the sender as a UITableViewCell, but you call performSegue() with sender as a IndexPath.
You need a cell by calling:
let cell = tableView.cellForRow(at: indexPath)
And then you can perform a segue:
performSegue(withIdentifier: "popOutNoteExpanded", sender: cell)