如何在View controller中使用More menucontroller

How to use More menucontroller in View controller

任何人都可以帮助我,我在这里遇到 UIMenucontroller.In 问题,我必须在单个 viewcontroller 中使用两个菜单控制器。 仅限第一个菜单 "paste",其他菜单 "copy","select","select all" 当我使用共享菜单控制器时,它会影响其他菜单。

我的第一个菜单代码如下:

override func canBecomeFirstResponder() -> Bool
{
    return true
}

override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool
{

  //actions

}

 UIMenuController.sharedMenuController().menuItems = nil

    let Select: UIMenuItem = UIMenuItem(title: "Select", action: Selector("Select"))
    let SelectAll: UIMenuItem = UIMenuItem(title: "SelectAll", action: Selector("SelectAll"))
    let Copy: UIMenuItem = UIMenuItem(title: "Copy", action: Selector("Copy"))


    let menu: UIMenuController = UIMenuController.sharedMenuController()
    menu.menuItems = [Select,SelectAll,Copy]
    menu.setTargetRect(cell.frame, inView: cell.superview!)
    menu.setMenuVisible(true, animated: true)

我的第二个菜单是:

 UIMenuController.sharedMenuController().menuVisible = false
 let paste: UIMenuItem = UIMenuItem(title: "Paste", action: Selector("paste"))

let menu: UIMenuController = UIMenuController.sharedMenuController()
menu.menuItems = [paste]   
menu.setTargetRect(message_Textfield.frame, inView:   message_Textfield.superview!)
menu.setMenuVisible(true, animated: true)

错误: 在这里,第二个菜单包含不需要的东西,如 [Select、SelectAll、Copy] 和 [Paste]。

我该如何解决这个问题,在此先感谢

您应该在 UITextField 子类中覆盖 canPerformAction 以禁用您不需要的项目,然后将您创建的每个 uitextfield 分配给子类。

例如uimenucontroller禁用粘贴菜单项:

class CustomTextField: UITextField {
    override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
        if action == "paste:" {
            return false
        }

        return super.canPerformAction(action, withSender: sender)
    }
}

用法:

let message_Textfield = CustomTextField()

现在 paste 菜单项将为 message_Textfield

禁用

您好感谢您的回答,但我找到了如下解决方案:

override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool
    {

        if(MenuBool == true){
            if action == Selector("Copy") || action == Selector("star") || action == Selector("info") || action == Selector("forward") || action == Selector("Delete")
            {
                print("UIMenuController====>CellMenu")
                UIMenuController.sharedMenuController().menuVisible = false
                return true
            }
            print("UIMenuController====>Defaultmenu1")
            return false

        }else if MenuBool == false
        {
            print("UIMenuController====>Defaultmenu2")
            return false

        }else{
            print("UIMenuController====>DefaultmenuElse")

            return false
        }
    }

在,这种方式工作正常。

:):):)