Greg Peckory Greg Peckory - 8 months ago 94
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



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