0

Alright, I've looked up a lot of documentation on how the UIAlertController's work and I have a rather unique scenario. My application is being designed to work with a HID Bluetooth scanner. When I use:

preferredStyle: UIAlertControllerStyle.Alert

After I generate an alert that the item I have scanned is incorrect. Cool, alert is happening, problem is if I scan again ( which emulates keyboard input ), the return key is being sent to the alert and the alert is running the dismiss action.

If I use:

preferredStyle: UIAlertControllerStyle.ActionSheet

Then the alert is staying where it should be and ignoring scans while the alert window is up just how I want it.

So my question is, how do I capture the return key and prevent the Alert from calling the dismiss action? I'm a bit new with swift and I have a feeling the answer is simple, but I've tried half a dozen things that just isn't working.

If there is a setting to prevent all user input to the alert window or anything solution, I'm all for any method. I just rather not use the ActionSheet, and prefer to use the iOS alerts instead of creating my own screen. If this is not possible, I'm sure I can build my own 'alerts' window.

Code, that I'm calling from a simple Alerts class I made.

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)
}
}
Zork
  • 149
  • 1
  • 1
  • 11

1 Answers1

0

Try something like this.

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)
    }
Kuntal Gajjar
  • 792
  • 6
  • 12