如何在编辑时根据 TextField 内容更改标签文本
How to change label text according to TextField content while edited
我刚刚接触 Swift,我正在尝试学习一些非常基础的东西。
我正在努力实现这一目标:
我有一个 TextField 和一个 Label,我希望根据 TextField 文本(应该是国家/地区名称)将 Label 设置为特定的表情符号。
示例:
如果 TextField 包含单词 "Italy",我希望将 Label 设置为“”。
如果 TextField 包含单词 "Mexico",则 Label 应设置为“”。
这应该在编辑 TextField 时发生,而不是使用任何按钮来确认其中的文本。
这是我的(显然不起作用)代码。
@IBOutlet weak var emojiLabel: UILabel!
@IBOutlet weak var myTestField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
changeLabel()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func changeLabel() {
emojiLabel.text = ""
var countryName = myTestField.text
if (countryName == "Italia") {
emojiLabel.text = ""
print (emojiLabel)
}
}
感谢 Teetz 的评论,我做到了。
如果有人需要,这是我的工作代码:
编辑 1:我强烈建议使用 Greg 解决方案,标记为答案。
@IBOutlet weak var emojiLabel: UILabel!
@IBOutlet weak var myTestField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
myTestField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), for: UIControlEvents.editingChanged)
textFieldDidChange(myTestField)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@objc func textFieldDidChange(_ textField: UITextField) {
emojiLabel.text = ""
var countryName = myTestField.text
if (countryName == "Italia") {
emojiLabel.text = ""
}
}
你要做的第一件事是建立一个包含所有你想要的对的字典,例如:
//Put this line outside of viewDidLoad
var dictionary: [String: String] = [:]
//Put these lines inside of viewDidLoad
dictionary["Italy"] = ""
dictionary["Mexico"] = ""
//...Add more pairs to the dictionary
现在您已经定义了所有对,接下来要做的是设置文本字段的委托。这样做允许您在文本字段中输入文本时 运行 一些代码块。首先,在您的视图控制器 class 定义中,您希望符合 UITextFieldDelegate:
class ViewController: UIViewController, UITextFieldDelegate {
//...rest of the code in the view controller
}
然后,在您的 viewDidLoad 中,您想将文本字段的委托设置为 self (a.k.a。到这个视图控制器,我们刚刚符合 UITextFieldDelegate):
override func viewDidLoad() {
super.viewDidLoad()
//...rest of code in viewDidLoad
myTestField.delegate = self
}
现在,所有这些都已完成,我们要调用此函数,只需开始输入文本字段,它就会自动完成,因为我们已经符合 UITextFieldDelegate:
func textFieldDidEndEditing(_ textField: UITextField) {
//...This function is called EVERY time editing is finished in myTestField
}
好的,所以在 textFieldDidEndEditing 函数内部,我们需要做的就是检查在 myTestField 中输入的内容,看看它是否与我们创建的字典中的任何键匹配在第一步中...如果是这样,那么我们最终将更新我们的标签:
func updateLabel(textField: UITextField) {
//we are using guard here because we are dealing with optional values,
//meaning that textField.text doesn't necessarily have a value, and
//dictionary[text] may not actually have a value either.
guard let text = textField.text,
let labelText = dictionary[text] else {return}
//Now we are going to actually update our label
emojiLabel.text = labelText
}
好的,看起来不错,最后要做的就是在 textFieldDidEndEditing 函数中调用我们的 updateLabel 函数:
func textFieldDidEndEditing(_ textField: UITextField) {
//...This function is called EVERY time editing is finished in myTestField
updateLabel(textField: textField)
}
就是这样!
我刚刚接触 Swift,我正在尝试学习一些非常基础的东西。
我正在努力实现这一目标:
我有一个 TextField 和一个 Label,我希望根据 TextField 文本(应该是国家/地区名称)将 Label 设置为特定的表情符号。
示例:
如果 TextField 包含单词 "Italy",我希望将 Label 设置为“”。
如果 TextField 包含单词 "Mexico",则 Label 应设置为“”。
这应该在编辑 TextField 时发生,而不是使用任何按钮来确认其中的文本。
这是我的(显然不起作用)代码。
@IBOutlet weak var emojiLabel: UILabel!
@IBOutlet weak var myTestField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
changeLabel()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func changeLabel() {
emojiLabel.text = ""
var countryName = myTestField.text
if (countryName == "Italia") {
emojiLabel.text = ""
print (emojiLabel)
}
}
感谢 Teetz 的评论,我做到了。 如果有人需要,这是我的工作代码:
编辑 1:我强烈建议使用 Greg 解决方案,标记为答案。
@IBOutlet weak var emojiLabel: UILabel!
@IBOutlet weak var myTestField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
myTestField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), for: UIControlEvents.editingChanged)
textFieldDidChange(myTestField)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@objc func textFieldDidChange(_ textField: UITextField) {
emojiLabel.text = ""
var countryName = myTestField.text
if (countryName == "Italia") {
emojiLabel.text = ""
}
}
你要做的第一件事是建立一个包含所有你想要的对的字典,例如:
//Put this line outside of viewDidLoad
var dictionary: [String: String] = [:]
//Put these lines inside of viewDidLoad
dictionary["Italy"] = ""
dictionary["Mexico"] = ""
//...Add more pairs to the dictionary
现在您已经定义了所有对,接下来要做的是设置文本字段的委托。这样做允许您在文本字段中输入文本时 运行 一些代码块。首先,在您的视图控制器 class 定义中,您希望符合 UITextFieldDelegate:
class ViewController: UIViewController, UITextFieldDelegate {
//...rest of the code in the view controller
}
然后,在您的 viewDidLoad 中,您想将文本字段的委托设置为 self (a.k.a。到这个视图控制器,我们刚刚符合 UITextFieldDelegate):
override func viewDidLoad() {
super.viewDidLoad()
//...rest of code in viewDidLoad
myTestField.delegate = self
}
现在,所有这些都已完成,我们要调用此函数,只需开始输入文本字段,它就会自动完成,因为我们已经符合 UITextFieldDelegate:
func textFieldDidEndEditing(_ textField: UITextField) {
//...This function is called EVERY time editing is finished in myTestField
}
好的,所以在 textFieldDidEndEditing 函数内部,我们需要做的就是检查在 myTestField 中输入的内容,看看它是否与我们创建的字典中的任何键匹配在第一步中...如果是这样,那么我们最终将更新我们的标签:
func updateLabel(textField: UITextField) {
//we are using guard here because we are dealing with optional values,
//meaning that textField.text doesn't necessarily have a value, and
//dictionary[text] may not actually have a value either.
guard let text = textField.text,
let labelText = dictionary[text] else {return}
//Now we are going to actually update our label
emojiLabel.text = labelText
}
好的,看起来不错,最后要做的就是在 textFieldDidEndEditing 函数中调用我们的 updateLabel 函数:
func textFieldDidEndEditing(_ textField: UITextField) {
//...This function is called EVERY time editing is finished in myTestField
updateLabel(textField: textField)
}
就是这样!