通过单击表格视图中的单元格打开新的视图控制器——Swift iOS

时间:2021-04-15 22:23:11

I have the following onClick function

我有下面的onClick函数

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

我只是想知道如何设置一个函数,它将引导你到新的视图控制器

4 个解决方案

#1


51  

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.

故事板:首先你需要为你的视图设置一个标识符。在下面的屏幕截图中,您可以看到在哪里输入标识符。

通过单击表格视图中的单元格打开新的视图控制器——Swift iOS

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:

Segue:首先,你必须为Segue设置一个标识符,如下所示:

通过单击表格视图中的单元格打开新的视图控制器——Swift iOS

通过单击表格视图中的单元格打开新的视图控制器——Swift iOS

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

编辑:更新为Swift 3.x

#2


7  

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)

}

#3


1  

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.

对于这个问题的其他用户来说,如果你在UITableViewController中有一个静态单元,你可以从单元格control拖动到新的视图控制器。

通过单击表格视图中的单元格打开新的视图控制器——Swift iOS

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.

如果不需要将任何数据传递给下一个视图控制器,也可以在动态单元上进行此操作。但是,如果您确实需要传递数据,那么应该从表视图的视图控制器本身而不是单元格中进行segue。然后以编程方式调用segue。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.performSegue(withIdentifier: "mySegueIdentifier", sender: self)
}

#4


0  

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

选定行的值传递给另一个视图控制器的时间太晚了。最好在func prepareForSegue中传递选中的行,如下所示,首先声明一个全局变量,以便将值传递给其他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))

    }

#1


51  

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.

故事板:首先你需要为你的视图设置一个标识符。在下面的屏幕截图中,您可以看到在哪里输入标识符。

通过单击表格视图中的单元格打开新的视图控制器——Swift iOS

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:

Segue:首先,你必须为Segue设置一个标识符,如下所示:

通过单击表格视图中的单元格打开新的视图控制器——Swift iOS

通过单击表格视图中的单元格打开新的视图控制器——Swift iOS

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

编辑:更新为Swift 3.x

#2


7  

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)

}

#3


1  

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.

对于这个问题的其他用户来说,如果你在UITableViewController中有一个静态单元,你可以从单元格control拖动到新的视图控制器。

通过单击表格视图中的单元格打开新的视图控制器——Swift iOS

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.

如果不需要将任何数据传递给下一个视图控制器,也可以在动态单元上进行此操作。但是,如果您确实需要传递数据,那么应该从表视图的视图控制器本身而不是单元格中进行segue。然后以编程方式调用segue。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.performSegue(withIdentifier: "mySegueIdentifier", sender: self)
}

#4


0  

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

选定行的值传递给另一个视图控制器的时间太晚了。最好在func prepareForSegue中传递选中的行,如下所示,首先声明一个全局变量,以便将值传递给其他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))

    }