How to call (show) another View Controller when clicked on the cell of Table View in Swift

时光怂恿深爱的人放手 提交于 2020-01-17 04:48:04

问题


I'm new in Swift. I want to learn that, how to call another View Controller when the user clicks on cell of Table View. Thanks in advance.


回答1:


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"; will navigate too detailViewController




回答2:


In 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

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 :)



来源:https://stackoverflow.com/questions/28695969/how-to-call-show-another-view-controller-when-clicked-on-the-cell-of-table-vie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!