如何使用Swift将自定义上下文菜单项添加到UITextView?

时间:2014-09-15 16:14:07

标签: ios menu swift uitextview

我设法将UITextView子类化并禁用"定义"上下文菜单项。

class TextViewer: UITextView {

   // Overide, disable the "Define" contextual menu item
   override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool     {
       if action == "_define:" {
           return false;
       }
       return super.canPerformAction(action, withSender: sender)
   }

}

但是现在,我如何添加一个不同的/自定义菜单项取代它?我猜它可能是这样的,但它不太正确。

var menuController = UIMenuController.sharedMenuController()
var customMenuItem = UIMenuItem(title: "Lookup", action: "lookupWord")
menuController.menuItems?.append(customMenuItem)

感谢大家给我的任何帮助。 ; - )

1 个答案:

答案 0 :(得分:1)

没关系:

let mnuController = UIMenuController.sharedMenuController()
let lookupMenu = UIMenuItem(title: "Tshawb", action: "tshawb")
mnuController.menuItems = NSArray(array: [lookupMenu])

// This makes the menu item visible.
mnuController.setMenuVisible(true, animated: true)