【问题标题】:How to change label text according to TextField content while edited如何在编辑时根据 TextField 内容更改标签文本
【发布时间】:2018-08-09 20:00:42
【问题描述】:

我刚刚接触了 Swift,我正在尝试学习一些非常基础的东西。

我正在努力实现这一目标:

我有一个 TextField 和一个标签,我希望根据 TextField 文本(应该是国家名称)将标签设置为特定的表情符号。

例子:

如果 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)
    }

}

【问题讨论】:

标签: ios swift xcode label textfield


【解决方案1】:

你要做的第一件事是建立一个包含所有你想要的对的字典,例如:

    //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

现在您已经定义了所有对,接下来您要做的是设置文本字段的委托。这样做可以让您在文本输入到文本字段时运行一些代码块。首先,在您的视图控制器类定义中,您希望符合 UITextFieldDelegate

class ViewController: UIViewController, UITextFieldDelegate {
    //...rest of the code in the view controller
}

然后,在您的 viewDidLoad 中,您希望将文本字段的委托设置为 self(也就是这个视图控制器,我们刚刚符合 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)
}

就是这样!

【讨论】:

  • 我看到你找到了解决方案,但你真的应该至少使用字典来存储你的配对而不是检查条件
  • 非常感谢@Greg!这个解决方案绝对比我的更优雅、更高效。作为一个新的 Swift 程序员,我只能感谢你用你的代码向我展示的东西!再次,非常感谢!
  • 没问题,作为一个额外的挑战,您应该尝试通过将所有 UITextFieldDelegate 内容放入扩展视图控制器的单独 swift 文件中来重新组织此代码,这将使事情变得更加简洁,并且是养成的好习惯。如果您需要查看代码,请告诉我。
  • 很好的建议。我会自己试一试,如果需要帮助,我会告诉你。很高兴有像你这样的人格雷格。非常感谢。
【解决方案2】:

感谢 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 = "?"
    }

}

【讨论】:

    猜你喜欢
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-24
    • 2021-08-23
    • 1970-01-01
    • 2012-01-24
    • 1970-01-01
    相关资源
    最近更新 更多