相关疑难解决方法(0)

swift 2.0 - UITextFieldDelegate协议扩展无法正常工作

我正在尝试UITextFieldDelegate使用协议扩展在某些方法上添加默认行为,如下所示:

extension ViewController: UITextFieldDelegate {
    // Works if I uncommented this so I know delegates are properly set
//    func textFieldShouldReturn(textField: UITextField) -> Bool {
//        textField.resignFirstResponder()
//        return true
//    }
}

extension UITextFieldDelegate {
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()

        return true
    }
}
Run Code Online (Sandbox Code Playgroud)

你可能猜到,键盘永远不会消失.我真的不知道这里的问题在哪里.这是语言限制吗?有人已经成功做到了吗?

编辑:

正如@Logan建议的那样,默认协议的方法实现不适用于标记为的协议@objc.但是,UITextFieldDelegate有以下签名public protocol UITextFieldDelegate : NSObjectProtocol {...}

我测试默认实现NSObjectProtocol,它似乎工作正常:

protocol Toto: NSObjectProtocol {
    func randomInt() -> Int
}

extension Toto {
    func randomInt() -> …
Run Code Online (Sandbox Code Playgroud)

protocols ios uitextfielddelegate swift

20
推荐指数
1
解决办法
4982
查看次数

标签 统计

ios ×1

protocols ×1

swift ×1

uitextfielddelegate ×1