Greg Peckory Greg Peckory - 5 months ago 71
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

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.

screenshot

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)

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

segue1

segue2

performSegueWithIdentifier("segue", sender: self)

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if segue.identifier == "segue" {
        // Setup new view controller
    }
}