在文本字段中输入警报而不是键盘 - >光标停留在文本字段Swift2中

Alert instead of keyboard for input in textfield --> cursor stays in textfield Swift2

我正在尝试为文本字段的输入视图发出警报,因为我有最多。做出 3 种选择,我的客户希望它看起来像这样。

基本上没问题,我可以点击文本字段,从而显示警报。在文本字段中选择值得到正确更新后,光标停留在文本字段中(我假设因为点击,文本字段是第一响应者)

现在的问题是,当我在光标位于其中时再次点击同一个文本字段时,默认键盘会打开。那么我该怎么做才能阻止它这样做呢?

我在代码的不同位置尝试了以下操作:

resignFirstResponder()
becomeFirstResponder()

此外,当我当前使用键盘编辑另一个文本字段时,我能够在点击相应的文本字段时显示警报。这会导致键盘覆盖警报,因为发生这种情况时我也无法关闭键盘。

针对此的尝试:(写在其他文本字段的事件方法中

    if (alertView != nil) {
        alertView?.dismiss()
    } //Not very professional, but still in development :)

这里有一些重要的代码片段,可以帮助您了解我是如何构建它的:

该函数是textfield的EditingDidBegin事件

@IBAction func TextboxUnitEditing(sender: UITextField) {
    //One try of preventing the cursor (not working)
    tableView.endEditing(true)

    ...
    /* Unrelated Code here */
    ...

    alertView = SwiftAlertView(...)

            ...
    /* Unrelated Code here */
    ...

    alertView.show()
}

警报的委托方法

func alertView(alertView: SwiftAlertView, clickedButtonAtIndex buttonIndex: Int) {
    //buttonIndex == 1 means, cancel was tapped
    if (buttonIndex == -1) {
        currentCell = nil
        tableView.endEditing(true)
        return
    }

    ...
    /* Change value in textfield */
}

所以我的问题是,哪里出了问题?我需要调用什么来摆脱光标?如果您还有什么需要知道的,请告诉我,我可以提供更多信息。

提前致谢。

编辑:

已解决。而不是:

    self.tableView.endEditing(true)

我不得不这样做:

    dispatch_async(dispatch_get_main_queue(),{
        self.tableView.endEditing(true)
    })

您可以使用此扩展来完成您描述的工作,不需要文本框吗?你用线来称呼它。

  showInfo(message: error!.localizedDescription)

这是它背后的扩展。

protocol ShowsInfo {}

extension ShowsInfo where Self: UIViewController {
    func showInfo(title: String = "Info", message: String) {
    let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
    alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil))
    dispatch_async(dispatch_get_main_queue()) {
        self.presentViewController(alertController, animated: true, completion: nil)
    }
}
}

而不是

self.tableView.endEditing(true)

必须在主线程上完成

dispatch_async(dispatch_get_main_queue(),{
    self.tableView.endEditing(true)
})

否则将跳过此行