通过单击表视图中的单元格打开新的视图控制器 - Swift iOS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30773529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open new View Controller by clicking Cell in Table View - Swift iOS
提问by Greg Peckory
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
我只是想知道你将如何设置一个函数来引导你到新的视图控制器
回答by Eendje
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.
故事板:
首先,您必须为您的视图设置一个标识符。在下面的屏幕截图中,您可以看到在哪里输入标识符。
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设置一个标识符,如下所示:
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 更新
回答by Saurabh Prajapati
In storyboard, set storyboardId of your viewController in Identity Inspector.
在 storyboard 中,在 Identity Inspector 中设置 viewController 的 storyboardId。
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)
}
回答by Suragch
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.
对于遇到此问题的其他用户,如果您在 a 中有静态单元格UITableViewController
,则可以控制从单元格到新视图控制器的拖动。
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.
如果您不需要将任何数据传递给下一个视图控制器,您也可以在动态单元格上执行此操作。但是,如果您确实需要传递数据,那么您应该从 Table View 的视图控制器本身而不是 cell 进行 segue。然后以编程方式调用 segue。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "mySegueIdentifier", sender: self)
}
回答by Jaywant Khedkar
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 .
希望这对某些人有所帮助。
回答by L.N.Vu
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 prepareForSegueas 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))
}