[英]How to call (show) another View Controller when clicked on the cell of Table View in Swift
I'm new in Swift. 我是Swift的新手。 I want to learn that, how to call another View Controller when the user clicks on cell of Table View.
我想学习如何在用户单击“表格视图”的单元格时调用另一个视图控制器。 Thanks in advance.
提前致谢。
write following code : 编写以下代码:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("showItemDetail", sender: tableView)}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "showItemDetail" {
let indexPath:NSIndexPath = self.tableView.indexPathForSelectedRow()!
let detailVC:ItemDetailViewController = segue.destinationViewController as ItemDetailViewController
detailVC.item = items[indexPath.row] as Item
}
} }
add a viewController from storyboard and add a segue from tableview cell to viewController with identifier "showItemDetail"; 从情节提要中添加一个viewController,并从tableview单元格中添加一个序列到具有标识符“ showItemDetail”的viewController中; will navigate too detailViewController
将会浏览太多detailViewController
In First View Controller 在First View Controller中
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!)
{
if segue.identifier == "showItemDetail"
{
let indexPath:NSIndexPath = self.tableView.indexPathForSelectedRow()!
let destination = segue.destinationViewController as DetailViewController
destination.name = "llkin Elimov"
}
In second Detail View Controller 在第二个Detail View Controller中
public DetailViewController
{
var name = "name" as String //After you created the segue in main.storyboard, in name , you will get your parsed string automatically
}
Hope my code is clear for you :) 希望我的代码对您清楚:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.