ios 使用 Swift 实现 UITextFieldDelegate

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24171857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:50:06  来源:igfitidea点击:

Implementing UITextFieldDelegate with Swift

iosdelegatesswift

提问by Beanwah

I have my ViewController class which implements UITextFieldDelegate. I have no auto complete for the funcs such as textFieldShouldBeginEditing. Is this a bug in XCode 6? Here's my class implementation.

我有实现 UITextFieldDelegate 的 ViewController 类。我没有自动完成诸如 textFieldShouldBeginEditing 之类的功能。这是 XCode 6 中的错误吗?这是我的类实现。

class ViewController: UIViewController, UITextFieldDelegate

采纳答案by Jiaaro

Xcode 6 (Beta 1) is not currently supporting autocomplete for non-implemented protocol methods/properties (for Swift).

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

Your best bet is to <CMD> - clickon the protocol that isn't yet fully implemented to see what you're missing.

您最好的选择是<CMD> - click在尚未完全实施的协议上查看您缺少的内容。

回答by jayesh kavathiya

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
}

回答by Kiran K

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

回答by DogCoffee

A bit more swifty is ...

更快一点的是......

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

回答by Piyush Mathur

While using Swift Version 3.1 with the outlets of UITextFields, do mark the changes.

将 Swift 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;
    }
}

回答by Ananthu R Krishnan

// MARK:- ---> Textfield Delegates

// MARK:- ---> 文本域代表

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

回答by Giang

Swift 3

斯威夫特 3

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

extension YourNameViewController: UITextFieldDelegate {
    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
    }
}

回答by Jonathan Natie Klopper

I found a little work-around. Just go to file inspector and set type to Objective-C while you are editing the file. Auto-completion presents you Swift options.

我找到了一些解决方法。只需在编辑文件时转到文件检查器并将类型设置为 Objective-C。自动完成为您提供 Swift 选项。

Just switch the type back to Swift when you build.

只需在构建时将类型切换回 Swift。

回答by Giang

Swift 4:

斯威夫特 4:

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


extension YourNameViewController: UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        switch textField {
        case yourNameTextField:
            yourNameTextField.resignFirstResponder()
        default:
            break
        }
        return true
    }

    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
        return true
    }
}

回答by york

In my case by mistake I have added the delegate methods outside of the scope of the class implementation in swift and that restricts the delegate methods to be called.

在我的情况下,我错误地在 swift 类实现范围之外添加了委托方法,这限制了要调用的委托方法。