I am trying to make it when the users clicks on Button in a table view cell it takes them to a new view controller related to the current cell.
Asked
Active
Viewed 37 times
-1
-
check this: https://stackoverflow.com/questions/30736391/presenting-a-view-controller-with-a-button-in-a-uitableviewcell-programmatically – Alap Anerao Aug 16 '17 at 11:23
-
1Possible duplicate of [Presenting a View Controller with a button in a UITableViewCell programmatically (Swift)](https://stackoverflow.com/questions/30736391/presenting-a-view-controller-with-a-button-in-a-uitableviewcell-programmatically) – Tamás Sengel Aug 16 '17 at 12:14
1 Answers
0
Try the below code,
Do this in cellForRowAt indexPath,
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell_Identifier", for: indexPath) as! UITableViewCell
cell.myButton.isUserInteractionEnabled = true
cell.myButton.tag = indexPath.row
cell.myButton.addTarget(self, action: #selector(didTapMyButton(_:)), for: UIControlEvents.touchUpInside)
return cell
}
And here is the button action,
func didTapMyButton(_ sender : UIButton) {
let selectedIndex = sender.tag // You can get index here, so according to the index you can navigate to particular ViewController
let nextVC = self.storyboard?.instantiateViewController(withIdentifier: "UIViewController_identifier") as! UIViewController
self.present(nextVC, animated: true, completion: nil)
}
Hope it helps ..

Austin Michael
- 460
- 4
- 18