41

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

Greg Peckory
  • 7,700
  • 21
  • 67
  • 114

5 Answers5

66

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

Eendje
  • 8,815
  • 1
  • 29
  • 31
  • Apologies for my inexperience. How do I specify which View Controller I assign to the 'destination' constant? – Greg Peckory Jun 11 '15 at 08:07
  • Did you create the view controller in storyboard or programmatically? – Eendje Jun 11 '15 at 08:09
  • Just using the storyboard. I'm very new to this and am trying to keep it as simple as possible. Thanks for the help. – Greg Peckory Jun 11 '15 at 08:17
  • I've edited my answer. According to your comment, you should use the second example "Storyboard" or the third example "Segue". – Eendje Jun 11 '15 at 08:24
  • I get error, 'use of undeclared type 'RoomViewController'' after setting Storyboard ID as 'RoomViewController'. Why might this happen? – Greg Peckory Jun 11 '15 at 08:31
  • You'll have to use a string: `"RoomViewController"`. – Eendje Jun 11 '15 at 08:32
  • Perfect! I'm only getting used to this new way of programming. Thanks a million. – Greg Peckory Jun 11 '15 at 08:35
  • You can also use `present(destination, animated: true)`. But this implies that you take care of dismissing the view to return the the table view. – Stephane Paquet May 07 '18 at 22:31
  • @Eendje The segue works fine. However, it shows the next page as a popup, not the style as clicking on rows in Settings and then moving to next view transiting the page from left to right. – zs2020 Dec 11 '18 at 16:11
9

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)
}
Saurabh Prajapati
  • 2,348
  • 1
  • 24
  • 42
4

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)
}
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • just dragging from a static cell doesn't seem to work for me. I'm using simple "Show" segue. I also tried to specify it as "accessory action" ("disclosure indicator" in my case), to no avail – Antek Jan 12 '18 at 10:45
  • ok, apparently the trick was to use navigation controller as the parent for the table controller – Antek Jan 12 '18 at 12:17
  • Ctrl-drag from the (left-side) view controller to the new view controller. – BadmintonCat Oct 08 '18 at 07:11
1

Try This it's working for me,

   func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("You tapped cell number \(indexPath.section).")
    print("Cell cliked value is \(indexPath.row)")

    if(indexPath.row == 0)
    {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)

    let controller = storyboard.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController

    self.navigationController?.pushViewController(controller, animated: true)

    }
  }

Hope this will help for some one .

Jaywant Khedkar
  • 5,941
  • 2
  • 44
  • 55
-1

if using,

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)

The value of selected row pass to another view controller too late. You'd better to pass the selected row in the func prepareForSegue as below, at first, declare a global variable to pass value to other viewController

var globalVar:Int 

In a storyboard-based application, you will often want to do a little preparation before navigation

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
        let selectedIndex = self.tableView.indexPathForCell(sender as! UITableViewCell)
        globalVar = selectedIndex!.row
        print("tableView prepareForSegue: " + String(globalVar))

    }
Rajamohan S
  • 7,229
  • 5
  • 36
  • 54
L.N.Vu
  • 369
  • 4
  • 8