Protocol Not Working

I'm trying to transfer a tableview cell from my hockeydetailVC to my favouritesVC using a button and I can't transfer it due to some issue.

My hockeydetailVC

Code Block
protocol addThis {
  func addFav(data: CurrentPlayers)
}


I'm trying to transfer my item object
Code Block
var item: CurrentPlayers?
 
var delegate: addThis? = nil


Code Block
func sendData() {
    if delegate != nil {
      let data = item!
      delegate?.addFav(data: data)
      print("This is the data being transferred: \(data)")
    }
  }
   
  @IBAction func addToFav(_ sender: Any) {
    sendData()
    print("Favourite button Pressed")
  }


My favouritesVC

Code Block
 var currentFav: CurrentPlayers?


Code Block
func addFav(data: CurrentPlayers) {
    currentFav = data
  }

I have a segue for my button so I have this

Code Block
  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  if segue.identifier == "r" {
    let hockeyDetailVC: HockeyDetailVC = segue.destination as! HockeyDetailVC
    hockeyDetailVC.delegate = self
  }
 }


If the transfer works then my sendData() method up top would print a statement but my addToFav only prints "favourite button pressed".
Answered by ZoneX in 660866022
I'm going to close this thread.
@ZoneX
It is very difficult to help if you don't do exactly what we propose.

I proposed:
Code Block
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("segue identifier", segue.identifier)
if segue.identifier == "r" {
let hockeyDetailVC: HockeyDetailVC = segue.destination as! HockeyDetailVC
hockeyDetailVC.delegate = self
}
}

and tell what you get.
If you get something different from
segue identifier r
such as
segue identifier R
or
segue identifier SomeString
that means that "r" is not the identifier of this segue (or you don't call the right segue).
Then change "r" by what you got here after "segue identifier"

And what you did is very different for testing purpose

Code Block
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "r" {
print("segue identifier", segue.identifier) // You put in the if that we know is not called ! It is key to check you call the RIGHT segue
let hockeyDetailVC: HockeyDetailVC = segue.destination as! HockeyDetailVC
hockeyDetailVC.delegate = self
print("destination HockeyDetailVC is", hockeyDetailVC)
}
}

I can transfer the data using the prepare method like so in my hockeyDetailVC

Code Block
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "r" {
    let destinationController = segue.destination as! FavouritesVC
    destinationController.currentFav = item!
  }
  }

but I am still curious about why my protocol is not transferring the data I want to send. I copied

Code Block
   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   if segue.identifier == "r" {
     print("segue identifier", segue.identifier)
     let hockeyDetailVC: HockeyDetailVC = segue.destination as! HockeyDetailVC
     hockeyDetailVC.delegate = self
     print("destination HockeyDetailVC is", hockeyDetailVC)
   }
  }

It does not print the segue.identifier it prints the delegate is nil and then it just crashes again. It does not matter what I change my segue.identifier too.
Accepted Answer
I'm going to close this thread.
Ok, let close it.

It does not print the segue.identifier it prints the delegate is nil and then it just crashes again. It does not matter what I change my segue.identifier too.

But you did not show (unless I missed something), where is this print statement.
Protocol Not Working
 
 
Q