0

On the FirstVC I have a variable index:

var index = -1 

, which keeps the information of cell's indexPath.row so I can know in which cell the button was tapped. (Each cell has its own button)

I tried like this:
CellForRow:

cell.Btn.tag = indexPath.row cell.Btn.addTarget(self, action: #selector(detectButton), for: .touchUpInside) 

and then:

@objc func detectButton(sender: UIButton) { self.index = sender.tag } 

The secondVC is connected by the segue so I tried to send the index like this:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "MySegue" { let vc = segue.destination as! SecondVC vc.element = self.elements[index] } } 

It crashes there because the index was still -1. I thought that @objc func detectButton will be called before prepareForSegue but it is not.
Why? And is there any other solution to send the index to the secondVC?

2
  • What causes prepareForSegue to be called? Commented Nov 12, 2020 at 13:20
  • First time working with segues because I had to. Segue was defined in storyboard via button in cell. Commented Nov 12, 2020 at 13:24

1 Answer 1

2
You have to perform the segue *manually*:
  • Reconnect the segue from the view controller (rather than from the table view cell) to the destination controller

  • Delete var index = -1

  • Replace

    @objc func detectButton(sender: UIButton) { self.index = sender.tag } 

    with (note the underscore after the open parentheses)

    @objc func detectButton(_ sender: UIButton) { performSegue(withIdentifier: "MySegue", sender: sender.tag) } 
  • Replace

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "MySegue" { let vc = segue.destination as! SecondVC vc.element = self.elements[index] } } 

    with

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "MySegue" { let vc = segue.destination as! SecondVC let index = sender as! Int vc.element = self.elements[index] } } 

Edit:

Apparently in the current segue setup the button triggers the segue. If so delete var index = -1 and also the line cell.Btn.addTarget... (but not the line to assign the tag) and the entire method @objc func detectButton(_ sender: UIButton) { ... }.

In prepare(for segue get the tag from the sender parameter

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "MySegue" { let vc = segue.destination as! SecondVC let index = (sender as! UIButton).tag vc.element = self.elements[index] } } 
Sign up to request clarification or add additional context in comments.

10 Comments

let index = sender as! Int On that line I have a signal SIGABRT crash saying: Could not cast value of type 'UIButton' (0x1e64d07d8) to 'NSNumber' (0x1e648a658).
If the button is passed, write (sender as! UIButton).tag
ok, changed but still having a crash saying Could not cast value of type '__NSCFNumber' (0x1e647e978) to 'UIButton' (0x1e64d07d8).
Walt a minute, you can't have both error messages. I meant to replace only let index = sender as! Int with let index = (sender as! UIButton).tag
Thanks, but I replaced already. I am not having bot error messages, the newest one I got is a little different from the first one.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.