Tcacenco Daniel
Tcacenco Daniel

Reputation: 445

UIMenuItem disabled auto selector on method

I have a problem with create new UIMenuItem and assigning it a selector. Problem is it automatically call its selector without tapping it. This is my code:

let customMenuItem1 = UIMenuItem(title: "Salvează", action: Selector(showNote()))
    menuController.menuItems = NSArray(array: [customMenuItem1]) as? [UIMenuItem]

This is the method for appearance of menuitem:

 override func canPerformAction(action: Selector,withSender sender: AnyObject?) -> Bool
{
    if action == Selector(showNote())
    {
        return super.canPerformAction(action, withSender: sender)
    }

    return false

}

Thanks all.

Upvotes: 1

Views: 112

Answers (1)

Tcacenco Daniel
Tcacenco Daniel

Reputation: 445

In first 2 line of code exist mistake in swift:

let customMenuItem1 = UIMenuItem(title: "Salvează", action: Selector(showNote()))
menuController.menuItems = NSArray(array: [customMenuItem1]) as? [UIMenuItem]

at hear we have Selector on method this Selector mean he will be aumatically call method without wait for user to tap and for resolving this problem only we can put like this

 let customMenuItem1 = UIMenuItem(title: "Salvează", action: #selector(RulesDetailViewController.showNote))
    menuController.menuItems = NSArray(array: [customMenuItem1]) as? [UIMenuItem]

because #selector this parameters wait for touch and event for users.

Upvotes: 1

Related Questions