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!!