如何在Swift中单击Table View的单元格时调用(显示)另一个View Controller

时间:2015-02-24 12:36:15

标签: ios swift uiviewcontroller action tableview

我是斯威夫特的新人。我想了解一下,当用户点击Table View的单元格时如何调用另一个View Controller。 提前谢谢。

2 个答案:

答案 0 :(得分:2)

编写以下代码:

    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
}

}

从storyboard添加一个viewController,并从tableview单元格添加一个segue到viewController,标识符为" showItemDetail&#34 ;; 将导航太detailViewController

答案 1 :(得分:2)

在第一个视图控制器中

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"
}

在第二个详图视图控制器中

public DetailViewController
{
    var name = "name" as String //After you created the segue in      main.storyboard, in name , you will get your parsed string automatically

}

希望我的代码对你很清楚:)