【问题标题】:Prevent keyboard from automatically appearing with UIAlertController使用 UIAlertController 防止键盘自动出现
【发布时间】:2016-04-21 16:29:40
【问题描述】:

我在 Swift 中有一个 UIAlertController(警报风格),一切正常。但是,我添加到其中的UITextField 是一个可选字段,用户不需要在其中输入文本。问题是当我显示这个UIAlertController 时,键盘会与默认选择的文本字段同时出现。我不希望键盘出现,除非用户点击UITextField。如何做到这一点?

    let popup = UIAlertController(title: "My title",
        message: "My message",
        preferredStyle: .Alert)
    popup.addTextFieldWithConfigurationHandler { (optionalTextField) -> Void in
        optionalTextField.placeholder = "This is optional"
    }
    let submitAction = UIAlertAction(title: "Submit", style: .Cancel) { (action) -> Void in
        let optionalTextField = popup.textFields![0]
        let text = optionalTextField.text
        print(text)
    }
    let cancelAction = UIAlertAction(title: "Cancel", style: .Default, handler: nil)
    popup.addAction(cancelAction)
    popup.addAction(submitAction)
    self.presentViewController(popup, animated: true, completion: nil)

【问题讨论】:

  • 您必须在其他地方进行 becomeFirstResponder 调用。或者您可以在显示警报之后/之前调用 self.view endEditing:YES。

标签: ios swift uitextfield uikeyboard uialertcontroller


【解决方案1】:

这应该可以解决问题:

让你的 viewController 符合UITextFieldDelegate

popup.textFields![0].delegate 分配给self

popup.textFields![0]添加唯一标签(我在下面的例子中使用了999)

实现这个

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
  if textField.tag == 999 {
    textField.tag = 0
    return false
  }else{
    return true
  }
}

您的代码应如下所示:

    let popup = UIAlertController(title: "My title",
                                  message: "My message",
                                  preferredStyle: .Alert)
    popup.addTextFieldWithConfigurationHandler { (optionalTextField) -> Void in
        optionalTextField.placeholder = "This is optional"
    }
    popup.textFields![0].delegate = self
    popup.textFields![0].tag = 999
    let submitAction = UIAlertAction(title: "Submit", style: .Cancel) { (action) -> Void in
        let optionalTextField = popup.textFields![0]
        let text = optionalTextField.text
        print(text)
    }
    let cancelAction = UIAlertAction(title: "Cancel", style: .Default, handler: nil)
    popup.addAction(cancelAction)
    popup.addAction(submitAction)
    self.presentViewController(popup, animated: true, completion: nil)

【讨论】:

    【解决方案2】:

    我认为这是警报中文本字段的默认行为,也许可以考虑另一种设计,以便文本字段仅在必要时显示...

    现在,让我们绕过这个!

    当你添加 textField 时,让你的 viewController 成为委托并向它添加标签。

    例如

    popup.addTextFieldWithConfigurationHandler { (optionalTextField) -> Void in
        optionalTextField.placeholder = "This is optional"
        optionalTextField.delegate = self
        optionalTextField.tag = -1
    }
    

    然后实现 textFieldShouldBeginEditing()

    func textFieldShouldBeginEditing(textField: UITextField!) {   
        if textField.tag == -1 {
            textField.tag = 0
            return false
        } else {
            return true
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-22
      • 2017-12-26
      • 2020-11-11
      • 2018-07-26
      • 2018-01-15
      相关资源
      最近更新 更多