【问题标题】:How to present a UIAlertController with textfield, but that textfield must not be a first responder?如何呈现带有文本字段的 UIAlertController,但该文本字段不能是第一响应者?
【发布时间】:2017-06-26 04:01:00
【问题描述】:

我想展示一个带有文本字段的 UIAlertController。这个完成了。但我不希望它成为第一响应者。有没有办法用文本框呈现 UIAlertController,但文本框不应该是第一响应者?

  func showAlertData(data: String, at indexPath: IndexPath) {
        let title =  data
        let alert = UIAlertController(title: renameDocumentString, message: renameDocumentMessageString, preferredStyle: UIAlertControllerStyle.alert)
        alert.addTextField(configurationHandler: {(textField: UITextField) in
            textField.text = title
            textField.addTarget(self, action: #selector(self.textChanged(_: )), for: .editingChanged)
            textField.addTarget(self, action: #selector(self.textDidBegin(_: )), for: .editingDidBegin)
        })
        let cancel = UIAlertAction(title: cancelString, style: UIAlertActionStyle.cancel, handler: { (_) -> Void in
            alert.dismiss(animated: true, completion: nil)
        })
        let action = UIAlertAction(title: alertButtonOkString, style: UIAlertActionStyle.default, handler: { (_) -> Void in
            if let textfield = alert.textFields?.first {
            }
        })
        alert.addAction(cancel)
        alert.addAction(action)
        self.actionToEnable = action
        action.isEnabled = false
        self.present(alert, animated: true, completion: nil)
    }

此代码将带有文本字段的 UIAlertController 呈现为响应者。我不想在展示 UIAlertController 时成为它的响应者。

【问题讨论】:

  • 在当前完成块中辞职第一响应者?
  • 我也尝试过。但它第一次显示键盘,然后隐藏它。但我第一次也不想要键盘。
  • 您要编辑AlertController的UITextField的文本吗?
  • 是的,一旦我们点击文本字段,键盘就会出现,而不是默认情况下

标签: swift uitextfield uialertcontroller


【解决方案1】:

这不是理想的解决方案,但根据您的要求,在添加 AlertController 时禁用文本字段,并在呈现 AlertController 后再次启用。

 @IBAction func btnClick(_ sender: UIButton) {

    let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
    alert.addTextField { (textField) in

        //Disable textfield
        textField.isEnabled = false
    }

    let okAction = UIAlertAction(title: "Ok", style: .default)
    alert.addAction(okAction)

    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

    self.present(alert, animated: true) {

        //Enable textfield
        alert.textFields?.first?.isEnabled = true
    }
}

【讨论】:

    【解决方案2】:

    根据您的要求,这是一个完美的解决方案,请参考下面的代码

    @IBAction func btnClick(_ sender: UIButton) {
    
    let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
    alert.addTextField { (textField) in 
       DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
                alert.textFields![1].becomeFirstResponder()
            })
    }
    
    let okAction = UIAlertAction(title: "Ok", style: .default)
    alert.addAction(okAction)
    
    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
    
    self.present(alert, animated: true)}
    

    【讨论】:

      猜你喜欢
      • 2020-08-24
      • 2016-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-05
      • 2011-07-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多