Search code examples
iosswiftuimenucontrolleruimenuitem

How to use More menucontroller in View controller


Can Anyone help me, i'm having problem with UIMenucontroller.In here, i have to use two menucontroller in single viewcontroller. For First menu only "paste",for other menu "copy","select","select all" When i'm using shared menucontroller it affects the other menu.

My code for first menu is as follows:

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)

and my second menu is:

 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)

Error: In here,in second menu contains unwanted things as [Select,SelectAll,Copy] with [Paste].

How can i resolve this,thanks in advance


Solution

  • Hi Thanks for your answer,But i found solution as follows:

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

    In,this manner working fine.

    :):):)