简体   繁体   English

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

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

I'm attempting to make an alert the input view for a textfield, since i have max. 我正在尝试使警报输入文本字段,因为我有最大。 3 choices to make and my customer wants to have it look this way. 有3种选择,我的客户希望这种外观。

Basically no problem, i can tap on the textfield, resulting in showing the alert. 基本上没问题,我可以点击文本框,从而显示警报。 After choosing the value in the textfield gets updated correctly, but then, the cursor stays in the textfield (im assuming because of the tap, the textfield is first responder) 在文本字段中选择值之后,可以正确更新该值,但随后光标将停留在文本字段中(假设由于轻击,该文本字段是第一响应者)

The problem now is, when i tap on that very same textfield again while the cursor is in it, the default keyboard opens. 现在的问题是,当我在光标位于文本字段中的同时再次点击它时,默认键盘将打开。 So what can i do to stop it from doing this? 那么我该怎么做才能阻止它呢?

I have tried the following things on various locations in code: 我在代码中的不同位置尝试了以下操作:

resignFirstResponder()
becomeFirstResponder()

Also, when im currently editing another textfield with the keyboard, im able to show the alert when tapping the corresponding textfield. 另外,当我当前正在使用键盘编辑另一个文本字段时,当点击相应的文本字段时,我将无法显示警报。 this results in the keyboard covering the alert, since im also not able to close the keyboard when this occurs. 这会导致键盘覆盖警报,因为在发生这种情况时,im也无法关闭键盘。

Attempt against this: (Written in Event methods of other textfields 尝试这样做:(写在其他文本字段的Event方法中

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

Here are some important code snippets to understand how i built it up: 以下是一些重要的代码段,以了解我的构建方式:

This function is the EditingDidBegin Event of the 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()
}

Delegate method of the alert 警报的委托方法

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 */
}

So my question is, what goes wrong? 所以我的问题是,出了什么问题? What do i need to call to get rid of the cursor? 我需要调用什么来摆脱光标? If there is anything more you need to know, please tell me and i can provide more information. 如果您需要了解更多信息,请告诉我,我可以提供更多信息。

Thanks in advance. 提前致谢。

EDIT: 编辑:

Solved. 解决了。 Instead of: 代替:

    self.tableView.endEditing(true)

i had to do this: 我必须这样做:

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

You can use this extension to do the job you seem to be describing, no text boxes needed? 您可以使用此扩展名执行您似乎正在描述的工作,不需要文本框? You call it with the line. 您用线路称呼它。

  showInfo(message: error!.localizedDescription)

Here is the extension behind it. 这是其背后的扩展名。

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)
    }
}
}

Instead of 代替

self.tableView.endEditing(true)

It has to be done on the main thread 它必须在主线程上完成

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

Otherwise this line will be skipped 否则,此行将被跳过

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM