Swift UIAlertController禁用没有UITextField

时间:2016-10-18 13:47:24

标签: ios swift xcode uikit ios9.3

好吧,我已经查阅了很多关于UIAlertController如何工作的文档,而且我有一个相当独特的场景。我的应用程序设计用于HID蓝牙扫描仪。当我使用时:

preferredStyle: UIAlertControllerStyle.Alert

生成警告后,我扫描的项目不正确。很酷,警报正在发生,问题是如果我再次扫描(模仿键盘输入),返回键被发送到警报并且警报正在运行解雇操作。

如果我使用:

preferredStyle: UIAlertControllerStyle.ActionSheet

然后警报就会停留在应有的位置,并在警报窗口正常运行时忽略扫描。

所以我的问题是,如何捕获返回键并阻止Alert调用dismiss操作?我对swift有点新意,我觉得答案很简单,但是我已经尝试了六件不起作用的东西。

如果有设置阻止所有用户输入警报窗口或任何解决方案,我会全部采用任何方法。我只是不使用ActionSheet,而是更喜欢使用iOS警报而不是创建我自己的屏幕。如果无法做到这一点,我相信我可以建立自己的警报'窗口。

代码,我从一个简单的警报类中调用。

import UIKit

class Alerts {

var controller: UIViewController
var message: String
var title: String

init?(title: String, message: String, controller: UIViewController){
    if title.isEmpty || message.isEmpty {
        return nil
    }

    self.title = title
    self.message = message
    self.controller = controller
}

func save_alert(input_field:UITextField, callable: (Bool)->Void ){
    let action = UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default) {
        UIAlertAction in
        callable(false)
        input_field.enabled = true
        input_field.becomeFirstResponder()
        print("DISMISS CALLED")
    }
    let alert = UIAlertController(title: self.title,message:self.message,preferredStyle: UIAlertControllerStyle.Alert)

    alert.addAction(action)

    self.controller.presentViewController(alert,animated:true, completion: nil)
}
}

1 个答案:

答案 0 :(得分:0)

尝试这样的事情。

func save_alert(input_field:UITextField, callable: (Bool)->Void ){
    let action = UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default) {
        UIAlertAction in
        callable(false)
        input_field.enabled = true
        input_field.becomeFirstResponder()
        print("DISMISS CALLED")
        showAlert()
    }
    showAlert()
}

func showAlert() {
        let alert = UIAlertController(title: self.title,message:self.message,preferredStyle: UIAlertControllerStyle.Alert)

        alert.addAction(action)

        self.controller.presentViewController(alert,animated:true, completion: nil)
    }