用Swift实现UITextFieldDelegate

 feify_fei512_478 发布于 2023-01-09 17:20

我有我的ViewController类,它实现了UITextFieldDelegate.我没有像textFieldShouldBeginEditing这样的func自动完成.这是XCode 6中的错误吗?这是我的类实现.

class ViewController: UIViewController, UITextFieldDelegate

jayesh kavat.. 39

class ViewController: UIViewController,UITextFieldDelegate  //set delegate to class 

@IBOutlet var txtValue: UITextField             //create a textfile variable 

override func viewDidLoad() {
    super.viewDidLoad()
    txtValue.delegate = self                  //set delegate to textfile
}


func textFieldDidBeginEditing(textField: UITextField!) {    //delegate method

}

func textFieldShouldEndEditing(textField: UITextField!) -> Bool {  //delegate method
    return false
}

func textFieldShouldReturn(textField: UITextField!) -> Bool {   //delegate method
  textField.resignFirstResponder()

    return true
}


小智.. 22

Swift 3.0.1

 // UITextField Delegates
    func textFieldDidBeginEditing(_ textField: UITextField) {
    }
    func textFieldDidEndEditing(_ textField: UITextField) {
    }
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        return true;
    }
    func textFieldShouldClear(_ textField: UITextField) -> Bool {
        return true;
    }
    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
        return true;
    }
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        return true;
    }
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder();
        return true;
    }


DogCoffee.. 18

更开心的是......

@IBOutlet weak var nameTF: UITextField! { didSet { nameTF.delegate = self } }


Piyush Mathu.. 10

使用Swift Version 3.1和UITextFields的出口时,请标记更改.

import UIKit

class LoginViewController: UIViewController, UITextFieldDelegate {
 @IBOutlet var txtUserID: UITextField!
 @IBOutlet var txtPwd: UITextField!
 override func viewDidLoad() {
    super.viewDidLoad()

    txtUserID.delegate = self
    txtPwd.delegate = self
 }
 // UITextField Delegates
    func textFieldDidBeginEditing(_ textField: UITextField) {
        print("TextField did begin editing method called")
    }
    func textFieldDidEndEditing(_ textField: UITextField) {
        print("TextField did end editing method called\(textField.text!)")
    }
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        print("TextField should begin editing method called")
        return true;
    }
    func textFieldShouldClear(_ textField: UITextField) -> Bool {
        print("TextField should clear method called")
        return true;
    }
    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
        print("TextField should end editing method called")
        return true;
    }
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        print("While entering the characters this method gets called")
        return true;
    }
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        print("TextField should return method called")
        textField.resignFirstResponder();
        return true;
    }
}


Jiaaro.. 7

Xcode 6(Beta 1)目前不支持未实现的协议方法/属性的自动完成(对于Swift).

您最好的选择是使用 - click尚未完全实现的协议来查看您缺少的内容.

6 个回答
  • Xcode 6(Beta 1)目前不支持未实现的协议方法/属性的自动完成(对于Swift).

    您最好的选择是使用<CMD> - click尚未完全实现的协议来查看您缺少的内容.

    2023-01-09 17:21 回答
  • class ViewController: UIViewController,UITextFieldDelegate  //set delegate to class 
    
    @IBOutlet var txtValue: UITextField             //create a textfile variable 
    
    override func viewDidLoad() {
        super.viewDidLoad()
        txtValue.delegate = self                  //set delegate to textfile
    }
    
    
    func textFieldDidBeginEditing(textField: UITextField!) {    //delegate method
    
    }
    
    func textFieldShouldEndEditing(textField: UITextField!) -> Bool {  //delegate method
        return false
    }
    
    func textFieldShouldReturn(textField: UITextField!) -> Bool {   //delegate method
      textField.resignFirstResponder()
    
        return true
    }
    

    2023-01-09 17:21 回答
  • Swift 3.0.1
    
     // UITextField Delegates
        func textFieldDidBeginEditing(_ textField: UITextField) {
        }
        func textFieldDidEndEditing(_ textField: UITextField) {
        }
        func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
            return true;
        }
        func textFieldShouldClear(_ textField: UITextField) -> Bool {
            return true;
        }
        func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
            return true;
        }
        func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
            return true;
        }
        func textFieldShouldReturn(_ textField: UITextField) -> Bool {
            textField.resignFirstResponder();
            return true;
        }
    

    2023-01-09 17:22 回答
  • 更开心的是......

    @IBOutlet weak var nameTF: UITextField! { didSet { nameTF.delegate = self } }
    

    2023-01-09 17:22 回答
  • 使用Swift Version 3.1和UITextFields的出口时,请标记更改.

    import UIKit
    
    class LoginViewController: UIViewController, UITextFieldDelegate {
     @IBOutlet var txtUserID: UITextField!
     @IBOutlet var txtPwd: UITextField!
     override func viewDidLoad() {
        super.viewDidLoad()
    
        txtUserID.delegate = self
        txtPwd.delegate = self
     }
     // UITextField Delegates
        func textFieldDidBeginEditing(_ textField: UITextField) {
            print("TextField did begin editing method called")
        }
        func textFieldDidEndEditing(_ textField: UITextField) {
            print("TextField did end editing method called\(textField.text!)")
        }
        func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
            print("TextField should begin editing method called")
            return true;
        }
        func textFieldShouldClear(_ textField: UITextField) -> Bool {
            print("TextField should clear method called")
            return true;
        }
        func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
            print("TextField should end editing method called")
            return true;
        }
        func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
            print("While entering the characters this method gets called")
            return true;
        }
        func textFieldShouldReturn(_ textField: UITextField) -> Bool {
            print("TextField should return method called")
            textField.resignFirstResponder();
            return true;
        }
    }
    

    2023-01-09 17:22 回答
  • // MARK: - ---> Textfield代表

    func textFieldDidBeginEditing(textField: UITextField) {
    
        print("TextField did begin editing method called")
    }
    
    func textFieldDidEndEditing(textField: UITextField) {
    
        print("TextField did end editing method called\(textField.text)")
    }
    
    func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
    
        print("TextField should begin editing method called")
        return true;
    }
    
    func textFieldShouldClear(textField: UITextField) -> Bool {
    
        print("TextField should clear method called")
        return true;
    }
    
    func textFieldShouldEndEditing(textField: UITextField) -> Bool {
        print("TextField should end editing method called")
        return true;
    }
    
    
    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        print("While entering the characters this method gets called")
        return true;
    }
    
    
    func textFieldShouldReturn(textField: UITextField) -> Bool {
    
        print("TextField should return method called")
        textField.resignFirstResponder();
        return true;
    }
    

    2023-01-09 17:23 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有