如何在View控制器中使用更多menucontroller

时间:2016-06-11 11:24:03

标签: ios swift uimenucontroller uimenuitem

任何人都可以帮助我,我遇到了UIMenucontroller的问题。在这里,我必须在单个viewcontroller中使用两个menucontroller。 仅限第一个菜单"粘贴",其他菜单"复制","选择","选择全部"当我使用共享菜单控制器时,它会影响另一个菜单。

第一个菜单的代码如下:

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)

错误: 在这里,在第二个菜单中包含[选择,选择全部,复制]不需要的东西,使用[粘贴]。

我如何解决这个问题,提前谢谢

2 个答案:

答案 0 :(得分:0)

您应该覆盖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菜单项

答案 1 :(得分:0)

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

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

在,这种方式工作正常。

:):):)