简体   繁体   中英

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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM