I would like to prevent the UIAlertController
from dismissing.
I have a UIAlertAction
that simply appends a string into the UIAlertTextFie
EDIT:
Updated for Swift 3
So I actually got this to work. In short, it involves adding a gesture recognizer to the UIAlertController
that triggers before the dismissal occurs.
First, you create lazily loaded computed variables for your UIAlertController
and the UIAlertAction
you want to prevent from triggering on your view controller so that they're accessible by the gesture recognizer's selector method (self
in the selector insinuates that all of this is inside a view controller).
lazy var alert: UIAlertController = {
let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
alert.addTextField(configurationHandler: nil)
let appendAction = self.appendAction
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alert.addAction(appendAction)
alert.addAction(cancelAction)
let gestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(self.append(sender:)))
gestureRecognizer.minimumPressDuration = 0.0
alert.view.addGestureRecognizer(gestureRecognizer)
return alert
}()
lazy var appendAction: UIAlertAction = {
return UIAlertAction(title: "Paste Message", style: .default, handler: nil)
}()
Make sure your gesture recognizer above is a UILongPressGestureRecognizer
set with a minimum press duration of 0. That way you can access the state of the gesture (for when the user touches down) before the action is triggered fully. There you can disable the UIAlertAction
, implement your custom code, and reenable the action after the gesture has completed (user has touched up). See below:
@objc func append(sender: UILongPressGestureRecognizer) {
if sender.state == .began {
appendAction.isEnabled = false
} else if sender.state == .ended {
// Do whatever you want with the alert text fields
print(alert.textFields![0].text)
appendAction.isEnabled = true
}
}
Then you just present the UIAlertController
wherever.
@IBAction func showAlert(sender: AnyObject) {
self.present(alert, animated: true, completion: nil)
}
This is obviously a hack but there's no other way that I know of to achieve this without a hack since it's not meant to be achieved. For example, the gesture recognizer is tied to the UIAlertController
so the user can trigger that method if they tap anywhere on the alert (besides the cancel button).
ORIGINAL ANSWER:
This is as close as I could come to a hackaround. If there was some way to customize the dismissal transition time to nothing then you could set animated:
to false and it would look like the same alert, but I don't think it's possible
class ViewController: UIViewController {
@IBAction func alert(sender: AnyObject) {
let alert = UIAlertController(title: "title", message: "message", preferredStyle: .Alert)
alert.addTextFieldWithConfigurationHandler(nil)
let appendAction = UIAlertAction(title: "Append text", style: .Default) { _ in
var textField = alert.textFields![0] as UITextField
// Append text here
self.presentViewController(alert, animated: true, completion: nil)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
alert.addAction(appendAction)
alert.addAction(cancelAction)
self.presentViewController(alert, animated: true, completion: nil)
}
}
I'm only familiar with swift
Pretty much the same question is answered here
Text field on alert supports paste option, so there is no real reason to have separate button on alert to indicate "paste" option.
Otherwise you should mimic UIAlertController and reimplement it with desiread behavior.