2016-10-18 21 views
0

好吧,我查了很多關於UIAlertController的工作方式和我有一個相當獨特的場景的文檔。我的應用程序正在設計用於HID藍牙掃描儀。當我使用:Swift UIAlertController禁用沒有UITextField的返回鍵

preferredStyle: UIAlertControllerStyle.Alert 

當我生成一個警告,說我掃描的項目不正確。酷,警報正在發生,問題是如果我再次掃描(模擬鍵盤輸入),返回鍵正在發送到警報和警報正在運行dismiss行動。

如果我使用:

preferredStyle: UIAlertControllerStyle.ActionSheet 

然後警報住的地方應該和忽略掃描,同時警告窗口了,我只是怎麼想的那樣。

所以我的問題是,我如何捕獲返回鍵,並防止警報調用dismiss動作?我有點新的快速,我有一種感覺,答案很簡單,但我已經嘗試了六件事情,但沒有成功。

如果有一個設置來防止所有用戶輸入到警報窗口或任何解決方案,我都是爲任何方法。我只是不使用ActionSheet,而更喜歡使用iOS警報,而不是創建自己的屏幕。如果這是不可能的,我相信我可以建立自己的'警報'窗口。

代碼,我從一個簡單的Alerts類中調用。

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

回答

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