How to call (show) another View Controller when cl

2019-09-01 06:10发布

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.

2条回答
可以哭但决不认输i
2楼-- · 2019-09-01 06:46

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

查看更多
ゆ 、 Hurt°
3楼-- · 2019-09-01 06:55

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

查看更多
登录 后发表回答