上午,
我需要帮助。我想要显示我想显示UItextfield错误消息UImenuController错误但这不起作用。我的UItextfield位于UITaBleView单元格中。!
我的班级
@IBOutlet weak var passwordField: UITextField!
func textFieldDidEndEditing(textField: UITextField) {
if textField == passwordField && !Validation.isValidPassword(textField.text){
displayMenu(textField, errorMessage : "error message")
}
}
func displayMenu(view : UITextField , errorMessage : String){
let menuItem : UIMenuItem = UIMenuItem(title: errorMessage , action: Selector("showMenu"))
var menu : UIMenuController = UIMenuController()
menu.arrowDirection = UIMenuControllerArrowDirection.Up
menu.menuItems = [menuItem as AnyObject]
menu.setTargetRect(CGRect(x: 0, y: 0, width: 100, height: 50), inView: self.tableView)
menu.setMenuVisible(true, animated: true)
println("display menu")
}
func showMenu() {
println("menu show");
}
override func canBecomeFirstResponder() -> Bool {
return true
}
override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
if action == Selector("showMenu") {
return tableView.canPerformAction(action, withSender: sender)
}
return false
}
错误
2015-02-21 17:54:03.576 Lovia[7870:177566] *** Assertion failure in -[UIMenuController init], /SourceCache/UIKit_Sim/UIKit-3347.6.1/UIMenuController.m:90
2015-02-21 17:54:03.585 Lovia[7870:177566] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'There can only be one UIMenuController instance.'
*** First throw call stack:
(
0 CoreFoundation 0x00000001026e4b95 __exceptionPreprocess + 165
1 libobjc.A.dylib
由于
答案 0 :(得分:0)
如果您需要在单元格中显示MenuItem,可以查看:How to show a custom UIMenuItem for a UITableViewCell?
canPerformAction 中的另一个问题:
override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
if action == Selector("showMenu") {
return true
}
return false
}