2016-10-18 29 views
0

UIAlertControllerの仕組みと独自のシナリオについては、多くのドキュメントを参照しています。私のアプリケーションは、HID Bluetoothスキャナで動作するように設計されています。私が使用しているとき:Swift UIAlertController UITextFieldを使用しないリターンキーを無効にする

preferredStyle: UIAlertControllerStyle.Alert 

私がスキャンしたアイテムが間違っているというアラートを生成した後。クール、アラートが発生している、キーボードの入力をエミュレートするもう一度スキャンすると、リターンキーがアラートに送信され、アラートがディスマイスアクションを実行しているという問題があります。

私が使用している場合:

preferredStyle: UIAlertControllerStyle.ActionSheet 

それがあるべき場所アラートが滞在していると警告ウィンドウが、私はそれをしたいどれだけアップしている間、スキャンを無視します。

私の質問は、どのようにリターンキーを取得し、アラートがディスマイズアクションを呼び出さないようにするかです。私は素早く少し新しく、答えはシンプルだと感じていますが、うまくいきませんでした。

アラートウィンドウやその他のソリューションへのすべてのユーザー入力を禁止する設定がある場合、私はすべての方法に対応しています。私は単に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) 
} 
} 

答えて

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) 
    } 
関連する問題