Respond to long press menu on UITableViewCell in class outside of table view

I need to popup a menu on a cell in a table view, but to respond to the action by specifying a selector on an object held elsewhere in the app.


I have written the following code in the AppDelegate :


    let testMenuItem = UIMenuItem(title: "Test", action: #selector(ValueModelCellPresenter.test(sender:)))
    
    UIMenuController.shared.menuItems = [testMenuItem]
    
    UIMenuController.shared.update()


I am implementing the shouldShowMenuForRowAt, canPerformAction and performAction methods on the table view delegate, as recommended, but the menu is not showing on long press.


I have also implemented :


  public override var canBecomeFirstResponder: Bool
  {
    return true
  }
  
  public override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool
  {
    return action == #selector(ValueModelCellPresenter.test(sender:))
  }

  @objc public func test(sender: Any)
  {
    print("here")
  }


… in the ValueModelCellPresenter class.


I also tried calling becomeFirstResponder() on the presenter but to no avail.


Can anyone please help me to see what I may have missed?

Replies

OK. I've finally got the menu to show by implementing canBecomeFirstResponder and canPerformAction on the cell class - Yaay!!


Now, I'm getting a crash when I tap on the menu item.


Does this have anything to do with how I am fulfilling the selector on a class outside of the table view / controller / cell / etc?


Could this have anything to do with the fact that I have one ValueModelCellPresenter for each cell (around a dozen on the current test table view)?