1

I have an UIAlertController and i'm checking for user input. When the user doesn't type in text field, the OK Action button i added should give the user a warning and not close the alert view.

I handle the warning but the alert view closes automatically. How can i disable the automatic closing?

Thanks.

MY CODE:

    var alert = UIAlertController(title: "change name and phone number", message: nil, preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default,
        handler: {  action in
            //Add a comment to this line

            let nameField: UITextField = alert.textFields![0] as UITextField
            let phoneField: UITextField = alert.textFields![1] as UITextField
            let name = nameField.text
            let phone = phoneField.text
            if name.length == 0 {
                JLToast.makeText("Please enter name").show()
            } else if phone.length == 0 {
                 JLToast.makeText("Please enter phone number").show()
            } else {
                self.sendSupportInfo(nameField.text, phone: phoneField.text)
            }
            println("name:: \(nameField.text), phone: \(phoneField.text)")
    }))
    alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: nil))

    alert.addTextFieldWithConfigurationHandler { (textField) -> Void in
        textField.placeholder = "name"
    }
    alert.addTextFieldWithConfigurationHandler { (textField) -> Void in
        textField.placeholder = "0544-444444"
        textField.keyboardType = UIKeyboardType.PhonePad
    }
    self.presentViewController(alert, animated: true, completion: nil)
F. Suyuti
  • 327
  • 3
  • 18
ilan
  • 4,402
  • 6
  • 40
  • 76
  • 1
    possible duplicate of [Prevent UIAlertController to dismiss](http://stackoverflow.com/questions/28919670/prevent-uialertcontroller-to-dismiss) – soulshined Apr 02 '15 at 20:52

2 Answers2

0

You can not do that. Other option is, you can only disable the UIAlertAction. Instead, you might want to create your own custom dialog to that.

as the offical documentation say:

Subclassing The UIAlertController class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

Dedy Khaidir
  • 33
  • 1
  • 7
0

U can create a new UI window and make the window level above the alert level and then show your alert onto the new UIwindow.

Developer Sheldon
  • 2,140
  • 1
  • 11
  • 17