I have the following onClick function
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let row = indexPath.row
println("Row: \(row)")
println(meetingArray[row] as! String)
}
which prints out the text on the cell which is clicked. It is working fine.
I'm just wondering how you would set a function which would direct you to the new view controller
Programmatically:
let destination = UIViewController() // Your destination navigationController?.pushViewController(destination, animated: true)
Storyboard:
First you'll have to set an identifier for your view. In the screenshot below you can see where to enter the identifier.
After that, you can create a "destination" and push it to the navigation controller by using the code below:
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main) let destination = storyboard.instantiateViewController(withIdentifier: "YourViewController") as! YourViewController navigationController?.pushViewController(destination, animated: true)
Segue:
First you'll have to set an identifier for your segue as shown below:
performSegue(withIdentifier: "segue", sender: self) override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "segue" { // Setup new view controller } }
EDIT: Updated for Swift 3.x
In storyboard, set storyboardId of your viewController in Identity Inspector.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let row = indexPath.row
println("Row: \(row)")
println(meetingArray[row] as! String)
let secondViewController = self.storyboard.instantiateViewControllerWithIdentifier("storyBoardIdFor your new ViewController") as SecondViewController
self.navigationController.pushViewController(secondViewController, animated: true)
}
For other users coming to this question, if you have a static cells in a UITableViewController
, you can just control drag from the cell to the new View Controller.
You can also do this on dynamic cells if you don't need to pass any data to the next View Controller. However, if you do need to pass data then you should make the segue from the Table View's view controller itself and not the cell. Then call the segue programmatically.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "mySegueIdentifier", sender: self)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With