Greg Peckory Greg Peckory - 2 years ago 189
Swift Question

Open new View Controller by clicking Cell in Table View - Swift iOS

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

Answer Source


let destination = UIViewController() // Your destination
navigationController?.pushViewController(destination, animated: true)

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: NSBundle.mainBundle())
let destination = storyboard.instantiateViewControllerWithIdentifier("YourViewController") as! YourViewController
navigationController?.pushViewController(destination, animated: true)

First you'll have to set an identifier for your segue as shown below:



performSegueWithIdentifier("segue", sender: self)

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if segue.identifier == "segue" {
        // Setup new view controller
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download