Swift - Delegates - How do I link the protocol to the implemented delegate methods?

1.2k Views Asked by At

I want to use a delegate to make my cells (from a UICollectionView) communicate with my ViewController.

In my Cell.swift file, I am declaring the protocol needed like this (outside the Cell class):

protocol CellDelegate: class {
    func someMethod(param1: String?, param2 param2: Bool)
}

In the same file I am declaring the delegate as follows:

class Cell: UICollectionViewCell {
     weak var delegate: CellDelegate?

     // ... some code ...

     @IBAction func someAction(sender: AnyObject) {
             delegate?.someMethod(param1, param2: true)
     }
}

Now in my ViewController, I am implementing someMethod:

extension ViewController: CellDelegate {
     func someMethod(param1: String?, param2 param2: Bool) {

     // ... some code ...

     }
}

Problem : I can not link the protocol with its implementation, cmd + click in the protocol leads nowhere. In my @IBAction, someMethod is not crashing, but it does nothing.

I saw this topic about the subject, but I do not understand where to implement the Step 6.

Can you help me ?

Thank you for your time.

1

There are 1 best solutions below

5
On BEST ANSWER

You are missing the final step: populating the delegate property of the Cell class. I usually do that in cellForRowAtIndexPath:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = self.table.dequeueReusableCellWithIdentifier("myCellId") as! Cell
    cell.delegate = self
    ...
    return cell
}

Note that there is no magic or automated behavior when using delegates:

  • a delegate is a protocol (your CellDelegate protocol)
  • you implement the protocol in the class you want to respond to the delegate invocations (you did in your ViewController class)
  • you create a delegate property in the class where the delegate methods are invoked (you did in your Cell class)
  • you initialize the delegate property with the actual instance of the class

You just missed the last step, leaving that property uninitialized, so any invocation using optional chaining evaluates to nil (like you did in the someAction method), and nothing happens.