Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

swift

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

like image 231
Greg Peckory Avatar asked Jun 11 '15 06:06

Greg Peckory


3 Answers

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: 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:

segue1

segue2

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

like image 127
Eendje Avatar answered Oct 04 '22 17:10

Eendje


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)
}
like image 26
Saurabh Prajapati Avatar answered Oct 04 '22 17:10

Saurabh Prajapati


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.

enter image description here

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)
}
like image 35
Suragch Avatar answered Oct 04 '22 17:10

Suragch