swift2 单击“按钮”时UITextField不结束编辑(委托textFieldDidEndEditing)

cygmwpex  于 2022-11-06  发布在  Swift
关注(0)|答案(4)|浏览(187)

我在屏幕上有两个textFields和一个Submit button。用户在第一个textField中输入详细信息,然后在第二个textField中输入详细信息。
我的要求是在单击Submit button时结束编辑,并在这些textFields中打印用户输入。我在打印第二个textField的值时遇到了问题,因为当用户单击Submit button时,编辑似乎永远不会结束。
这是我的代码。感谢你在这个问题上的帮助(我已经添加了文本字段委托)

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    var firstName = ""
    var lastName = ""

    @IBOutlet var buttonUI: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func Submit(sender: UIButton) {

        print(firstName)
        print(lastName)

    }

    func textFieldDidEndEditing(textField: UITextField) {

        switch textField.tag {

        case 1:
              firstName = textField.text!
            print(firstName)
        case 2:

            lastName = textField.text!
            print(lastName)

        default: break
        }

    }

}
628mspwn

628mspwn1#

ViewController类中,为每个文本字段创建一个@IBOutlet属性,方法是按住Ctrl键并将每个文本字段从情节提要拖到代码中

@IBOutlet weak var textFieldFirst: UITextField!
@IBOutlet weak var textFieldSecond: UITextField!

另外,创建一个私有UITextField属性来保存(可能)当前(/最近使用的)文本字段。

private var currentTextField: UITextField?

重写UIViewController子类的viewDidLoad方法,以初始化两个“public”文本字段示例的委托

override func viewDidLoad() {
    super.viewDidLoad()

    // Handle the user input in the text fields through delegate callbacks (and set tags)
    textFieldFirst.delegate = self
    textFieldFirst.tag = 1
    textFieldSecond.delegate = self
    textFieldSecond.tag = 2
}

此外,使用UITextFieldDelegate的两个textFieldShouldReturn(...)textFieldDidBeginEditing(...)方法分别将(活动)文本字段的状态重新指定为第一响应者,并更新currentTextField引用。

// UITextFieldDelegate
func textFieldShouldReturn(textField: UITextField) -> Bool {
    // User finished typing (hit return): hide the keyboard.
    textField.resignFirstResponder()
    return true
}

func textFieldDidBeginEditing(textField: UITextField) {
    currentTextField = textField
}

最后,如果在编辑过程中按下submit,则将任何可能的当前文本字段重新指定为第一响应者

@IBAction func Submit(sender: UIButton) {
    if let currentTextField = currentTextField {
        currentTextField.resignFirstResponder()
    }
    print(firstName)
    print(lastName)
}

这样,您的视图控制器就可以正常工作了。
总结一下:添加和修改上述内容后,您的ViewController类应该如下所示

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    // Properties
    @IBOutlet weak var textFieldFirst: UITextField!
    @IBOutlet weak var textFieldSecond: UITextField!
    private var currentTextField: UITextField?
    var firstName = ""
    var lastName = ""

    @IBOutlet var buttonUI: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    // Actions
    @IBAction func Submit(sender: UIButton) {
        if let currentTextField = currentTextField {
            currentTextField.resignFirstResponder()
        }
        print(firstName)
        print(lastName)
    }

    // viewDidLoad
    override func viewDidLoad() {
        super.viewDidLoad()

        // handle the user input in the text fields through delegate callbacks
        textFieldFirst.delegate = self
        textFieldSecond.delegate = self

        // tags
        textFieldFirst.tag = 1
        textFieldSecond.tag = 2
    }

    // UITextFieldDelegate
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        // User finished typing (hit return): hide the keyboard.
        textField.resignFirstResponder()
        return true
    }

    func textFieldDidBeginEditing(textField: UITextField) {
        currentTextField = textField
    }

    func textFieldDidEndEditing(textField: UITextField) {
        switch textField.tag {
        case 1:
            firstName = textField.text!
            print(firstName)
        case 2:
            lastName = textField.text!
            print(lastName)
        default: break
        }
    }
}
yvgpqqbh

yvgpqqbh2#

什么叫
我在打印第二个文本字段的用户输入时遇到问题,因为用户单击“提交”时编辑从未结束。
为什么不在用户按提交时读取文本字段中的值呢?请确保您的插座和代表是正确的。
对你的按钮Touch up inside做一个动作,在那个按钮里你会读到值:

firstName = textFieldOne.text
lastName = textFieldTwo.text
pzfprimi

pzfprimi3#

如果还有人需要的话,可以加

view.endEditing(true)

在你的纽扣里在牢房外
并将其添加到表viewCell中

func textFieldShouldReturn(textField: UITextField) -> Bool {
        // User finished typing (hit return): hide the keyboard.
        textField.resignFirstResponder()
        return true
}
4bbkushb

4bbkushb4#

我实施了dfri的建议,很有效!!!
导入UIKit
类视图控制器:UI视图控制器、UI文本字段委托{

var firstName = ""
var lastName = ""

@IBOutlet var NameTextField: UITextField!
@IBOutlet var PasswordTextField: UITextField!

@IBOutlet var buttonUI: UIButton!
override func viewDidLoad() {
    super.viewDidLoad()
}

@IBAction func Submit(sender: UIButton) {

    PasswordTextField.resignFirstResponder() // this works and dismisses the editing ( outlets need to be connected aswell for the textfields)
    print(firstName)
    print(lastName)

}

func textFieldDidEndEditing(textField: UITextField) {

    switch textField.tag {

    case 1:
          firstName = textField.text!
        print(firstName)
    case 2:

        lastName = textField.text!
        print(lastName)

    default: break
    }

}

}

相关问题