Swift 如何在 UIAlertController 中为 addTextField 设置默认文本

How to set default text for addTextField in UIAlertController in Swift

下面是我试过的代码。 textField.placeholder 完美运行,我需要为使用 UIAlertController 中的 addTextField 创建的文本字段设置默认文本。

      let getPinNameAlert = UIAlertController(title: title, message: message, preferredStyle: .alert)
      getPinNameAlert.addTextField(configurationHandler: { (textField) -> Void in
      // textField.placeholder = "Pin Name"
      textField.text = "Current Pin Name"
      textField.keyboardType = UIKeyboardType.default
      textField.clearsOnBeginEditing = true
    })

删除行textField.clearsOnBeginEditing = true

默认情况下,警报会自动聚焦于文本字段,这就是您设置的文本被清除的原因。