2016-06-11 74 views
0

任何人都可以幫助我,我遇到了UIMenucontroller的問題。在這裏,我必須在單個視圖控制器中使用兩個菜單控制器。 對於第一個菜單隻有「粘貼」,對於其他菜單「複製」,「選擇」,「全選」當我使用共享菜單控制器時,它會影響其他菜單。如何在視圖控制器中使用更多的菜單控制器

我對第一菜單代碼如下:

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]。

我怎樣才能解決這個問題,在此先感謝

回答

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

在這種方式工作正常。

:) :) :)

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被禁用

相關問題