gpt4 book ai didi

ios - 在用户输入有四个字符后,如何在 Swift 3 中调用准备方法?

转载 作者:搜寻专家 更新时间:2023-11-01 06:03:59 26 4
gpt4 key购买 nike

我有以下方法:

@IBAction func postcodeTextfieldDidChanged(_ sender: UITextField) {

if (postcodeTextField.text?.characters.count == 4) {
self.postcode = postcodeTextField.text
postcodeHas4Chars = true
} else {
postcodeHas4Chars = false
}
}

准备方法:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

if segue.identifier == "CoffeeShopListWithPostcode" {
if (postcodeHas4Chars == true) {
if let list = segue.destination as? CoffeeShopListVC {
list.postcode = postcode
} else {
print("Data NOT Passed!")
}
}
} else { print("Id doesnt match with Storyboard segue Id")
}
}

代码有效,我可以将邮政编码发送到下一个 ViewController,但是如何更改代码以在第四次输入后自动调用该方法?此时,用户必须手动退出输入才能执行。

最佳答案

每次文本字段的文本更改时,检查文本是否为 4 个字符。如果是,调用 performSegue(withIdentifier, sender:)。简单:

@IBAction func postcodeTextfieldDidChanged(_ sender: UITextField) {

if (postcodeTextField.text?.characters.count == 4) {
self.postcode = postcodeTextField.text
postcodeHas4Chars = true
performSegue(withIdentifier: "CoffeeShopListWithPostcode", sender: self)
} else {
postcodeHas4Chars = false
}
}

此外,请记住您连接到 editingChanged 事件而不是 valueChanged 事件

关于ios - 在用户输入有四个字符后,如何在 Swift 3 中调用准备方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42281326/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com