【问题标题】:AlertController TextField Input to Change Text in Label [duplicate]AlertController TextField 输入以更改标签中的文本[重复]
【发布时间】:2018-12-17 14:47:55
【问题描述】:

每当用户从 AlertController 向我的文本字段输入文本并点击 ENTER 按钮时,我都想更改我的标签“teamNameLabel”。

我真的很接近。我只需要输入的文本在点击 ENTER 按钮时显示为我的标签。

另外:有没有办法让输入到我的文本字段中的文本仅大写? (例如,用户必须输入“团队名称”而不是“团队名称”)

查看下面的代码和图片:

@IBAction func editTeamNameButton() {

    let message = "Please Enter Below"
    let alert = UIAlertController(title: "ENTER TEAM NAME", message: message, preferredStyle: .alert)

    let action = UIAlertAction(title: "ENTER", style: .default, handler: nil)


    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)


    alert.addAction(action)
    alert.addAction(cancelAction)
    alert.preferredAction = action


    alert.addTextField(configurationHandler: { (textField) in
        textField.placeholder = "Eg. TEAM NAME"



    })

    present(alert, animated: true, completion: nil)

    }

Click Here To View Screenshot Of My AlertController

【问题讨论】:

标签: ios swift uialertcontroller


【解决方案1】:

您必须为您的UIAlertAction 添加处理程序,在此处理程序中,您可以检查alerttextFields 中的第一个UITextField 是否存在,如果存在,您可以分配标签的text 属性作为此textFieldtext 属性

let action = UIAlertAction(title: "ENTER", style: .default) { _ in
    if let textField = alert.textFields?[0] {
        self.teamNameLabel.text = textField.text!
    }
}

【讨论】:

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