I have a long press gesture set on a UITableView
that presents a UIAlertController
containing the cell\'s text. When the UIAlertController
I have had the same issue. I was able to fix it by this code:
if self.presentedViewController == nil {
self.present(Alert, animated: true, completion: nil)
}
else {
self.dismiss(animated: false, completion: nil)
self.present(Alert, animated: true, completion: nil)
}
0
Dismiss the current controller and present the alert controller like
func alert(_ message:String) {
let alert = UIAlertController(title: "Error!", message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Dismiss", style: .default, handler: nil))
self.dismiss(animated: false, completion: nil)
self.present(alert, animated: true,completion: nil)
}
For some reason, both pieces of code are executing in the
if
That should have rung alarm bells for me. It is impossible that both the if
and the else
should run. This code must be running twice.
That is because you are not testing the state of the gesture recognizer. A long press g.r. sends its action message twice. You are running this code both on the long press and on the release. You need to test the state of the g.r. so that you don't do that. Example:
@IBAction func longPressedView(g: UIGestureRecognizer) {
if g.state == .Began {
// ... do it all here
}
}
You should differentiate the gesture state then execute the code you want, if not the selector you add to target will be executed first time when the gesture's state is UIGestureRecognizerStateBegan
and second time when the gesture's state is UIGestureRecognizerStateCancelled
, the second performance, alertController is showing, so Xcode will log warning.