-2

I am trying to implement when user click the tableView cell, that selected cell value need to pass detail viewcontroller. Here, the transition connection I made by storyboard (not programmatically) and present modally transition I used.

I would like to know how to do, so provide some example.

present modally transition

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

   // here I need implement get selected index path and passing values to another vc

} 
pastelsdev
  • 145
  • 1
  • 14
  • Hello @matt you should read my questions completely first. I asked for Swift and present modally transition. Your duplication reference different transition also it's objective C. please remove your mark. It will help lot of developers like me. – pastelsdev Nov 14 '18 at 17:59

1 Answers1

2

Use preparefor(segue:) as follows,

I'm assuming few thing

  1. Your current view controller is showing a list of player.
  2. You are showing a list of player.
  3. Tapping a player you wanted to show the player details.
  4. Your player details view controller name is PlayerDetailsVC.
  5. You a array of Players model, which are you showing in the tableview.
  6. From storyboard, your tableview selection mode is single selection.

Based on these assumption your code will be like following

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let indexPath = tableView.indexPathForSelectedRow{
        let selectedRow = indexPath.row
        let PlayerDetailsVC= segue.destination as! DetailViewController
        let member = "\(players[selectedRow].name ?? "")"
        PlayerDetailsVC.player = member
    }
}

In DetailViewcontroller need to do below

var player:String? //print it within viewdidload
pastelsdev
  • 145
  • 1
  • 14
Ratul Sharker
  • 7,484
  • 4
  • 35
  • 44
  • thank you for your answer. please provide more detail code.@Ratul Sharker – pastelsdev Nov 14 '18 at 17:54
  • Please check the answer, i've updated it. – Ratul Sharker Nov 14 '18 at 18:07
  • I am getting error on DetailViewController.player = teams[indexPath.row] Instance member 'player' cannot be used on type 'DetailViewController'. @Ratul Sharker – pastelsdev Nov 14 '18 at 18:25
  • 1
    Pay a close attention, to the code, here i am using an instance of `DetailViewController` not the class directly itself. – Ratul Sharker Nov 14 '18 at 18:27
  • Certainly! you can. You can instantiate the `DetailViewController` inject the `Player` model in it and then present the detail vc modally. But before doing that, please check your segue identifier and you are taking optional binding of the correct class. Another thing is to check, the detail vc in storyboard has the custom class set to `DetailViewController`. – Ratul Sharker Nov 14 '18 at 18:46