0

I'm trying to implement a delegate function where, when you click on a cell inside of a tableView, that tableView will call a delegate function on the initial ViewController. For some reason, that just doesn't work.

(I should note, that I have another delegate set up in a custom .xib file that will ask the original View Controller to perform the segue)

So the way I have my code implemented now is ViewControllerA loads up a tableView that has custom cells in it. Each of those custom cells has a button where I can call a delegate function that is placed in ViewControllerA to perform a segue to the third view controller called FCSViewController. When you click on a tableCell in the FCSViewController, it should call a delegate function in ViewControllerA before popping itself off of the stack, but never does.

Here's my implementation:

ViewControllerA:

class ViewController: UIViewController, UITableViewDataSource,UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!

let fcsViewController = FCSViewController()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
   
    fcsViewController.delegate = self
    
    tableView.dataSource = self
    tableView.delegate = self
    tableView.register(UINib(nibName: "hourlyEntry", bundle: nil), forCellReuseIdentifier: "TimeLabel")
    
    tableView.backgroundColor = .white
    
    tableView.rowHeight = UITableView.automaticDimension
    tableView.estimatedRowHeight = 90
    
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 24
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "TimeLabel", for: indexPath) as! hourlyEntry
    cell.timeLabel.text = "\(indexPath.row):00"
    cell.delegate = self

    return cell
}

func performSegueFromTableCell(withIdentifier identifier: String) {
    performSegue(withIdentifier: identifier, sender: nil)
}}

With an extension for ViewControllerA:

extension ViewController : FCSSelectorDelegate {
func editSelectedCell(FCSScore: Int, cellToEdit: Int) {
    print("get the damn delegate!")
}}

and here is my FCSViewController:

enter code hereprotocol FCSSelectorDelegate : class {
func editSelectedCell(FCSScore: Int, cellToEdit: Int)}

class FCSViewController: UITableViewController {

var editingCell = Int()

weak var delegate : FCSSelectorDelegate?

override func viewDidLoad() {
    super.viewDidLoad()

    
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 20
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.text = "blah"
    cell.textLabel?.numberOfLines = 0
    return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    delegate?.editSelectedCell(FCSScore: indexPath.row, cellToEdit: editingCell)
    print("Sending delegate")
    tableView.deselectRow(at: indexPath, animated: true)
    self.navigationController?.popViewController(animated: true)
}

}

The delegate function of ViewControlerA never fires. I also did the breakpoints thing from this question and I found that the delegate is set fine, (I'm assuming from seeing this in the debug menu: delegate INIM_Activty_Log.FCSSelectorDelegate? 0x00007fcb17a08a70, but when I do the breakpoint at the time when the delegate function is called from FCSViewController, the delegate is now nil. I ever tried to implement a breakpoint in the deinit function of ViewControllerA, but it never fired.

Thanks for your help! I want to be better with delegates, but they just don't like me!!

Jeff Cournoyer
  • 474
  • 2
  • 12

2 Answers2

1

This...

let fcsViewController = FCSViewController()

...creates an instance of FCSViewController that you never do anything with except set the delegate.

Since you are using a segue to display a FCSViewController instance, you need to set the delegate on the one that's the segue destination. Implement prepareForSegue and set the delegate there.

https://developer.apple.com/documentation/uikit/uiviewcontroller/1621490-prepareforsegue

Phillip Mills
  • 30,888
  • 4
  • 42
  • 57
1

Do you have a customCell class?, and so are you calling the delegate from that class?. Because the button clicked should have the function there and call the delegate.

These is how the custom class should look: CustomClass.Swift

// Define the protocols for custom cell
protocol customTableCellControllerProtocol: class {
    func editSelectedCell(FCSScore: Int, cellToEdit: Int)
}

// Custom cell class
class customTableCellController: UITableViewCell {

   var cellDelegate: ReviewCollectionViewCellProtocol?

   // FOLLOWING YOU SHOULD HAVE THE BUTTON FUNCTION
   // WHEN CLICKED WILL CALL THE PROTOCOL. FOR EXAMPLE:

   func theButton(_ sender: Any) {
        // With this line the extension protocols in ViewControllerAshould be properly activated. 
        cellDelegate?.editSelectedCell(FCSScore: Int, cellToEdit: Int)
    }


}

And in ViewController.swift

INSTEAD OF

extension ViewController : FCSSelectorDelegate {
func editSelectedCell(FCSScore: Int, cellToEdit: Int) {
    print("get the damn delegate!")
}}

CHANGE TO

extension ViewController : customTableCellControllerProtocol {
func editSelectedCell(FCSScore: Int, cellToEdit: Int) {
    // YOU CAN PERFORM THE SEGUE HERE
}}

Adding all that should trigger the extension protocol in the ViewControllerA. Please let me know if this works or more information in case I got the wrong idea. Good luck!

Cristian
  • 62
  • 4