I'm trying to add a custom action to UIMenuController
for use of on a UITableViewCell
and it doesn't appear when the menu is shown.
Edit: Revised code.
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
override func viewDidLoad() {
...
UIMenuController.shared.menuItems = [UIMenuItem(title: "Test", action: #selector(test))]
UIMenuController.shared.update()
}
// Table view setup
// ...
func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
return action == #selector(copy(_:)) || action == #selector(test)
}
func tableView(_ tableView: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) {
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return action == #selector(test)
}
@objc func test() {
print("Hello, world!")
}
}