Reputation: 3
I need a little help with a UIAlert. I get an error "UIAlertController can only have one action with a style of UIAlertActionStyleCancel"
. I know it is because I am initiating this alert outside of the function, but unsure how to fix it. How can I get access to the alert within the if blah < 80 {
conditional?
let alertView = UIAlertController(title: "Blah", message: "", preferredStyle: UIAlertControllerStyle.Alert)
@IBAction func blahButtonPressed(sender: AnyObject) {
alertView.addAction(UIAlertAction(title: "Do Something", style: .Default, handler: { (action: UIAlertAction!) in
// I have code here
}))
alertView.addAction(UIAlertAction(title: "Do Something 2", style: .Default, handler: { (action: UIAlertAction!) in
// I have code here
}))
alertView.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action: UIAlertAction!) in
// I have code here
}))
presentViewController(alertView, animated: true, completion: nil)
}
Later in the code I get values via Bluetooth and need to dismiss the Alert if a value is below 80.
if blah < 80 {
alertView.dismissViewControllerAnimated(true, completion: nil)
}
Upvotes: 0
Views: 890
Reputation: 1595
I'm not 100% but does it work when you only press the button once? If so, then it may be because you are adding the actions to your alertView
inside the @IBAction
. Instead, you may want to try moving the addition of the UIAlertAction
's outside of the @IBAction
and only presenting the alert view inside of it. Like so:
let alertView = UIAlertController(title: "Blah", message: "", preferredStyle: UIAlertControllerStyle.Alert)
alertView.addAction(UIAlertAction(title: "Do Something", style: .Default, handler: { (action: UIAlertAction!) in
// I have code here
}))
alertView.addAction(UIAlertAction(title: "Do Something 2", style: .Default, handler: { (action: UIAlertAction!) in
// I have code here
}))
alertView.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action: UIAlertAction!) in
// I have code here
}))
@IBAction func blahButtonPressed(sender: AnyObject) {
presentViewController(alertView, animated: true, completion: nil)
}
This way the UIAlertAction
's don't get added every single time the "blahButton" is pressed (which would result in more than one UIAlertAction
with style of "UIAlertActionStyleCancel")
Upvotes: 2
Reputation: 163
Here is how I fixed it if anyone comes accross this.
var newAlert: AnyObject?
@IBAction func blahButtonPressed(sender: AnyObject) {
let alertView = UIAlertController(title: "Blah", message: "", preferredStyle: UIAlertControllerStyle.Alert)
alertView.addAction(UIAlertAction(title: "Do Something", style: .Default, handler: { (action: UIAlertAction!) in
// I have code here
}))
alertView.addAction(UIAlertAction(title: "Do Something 2", style: .Default, handler: { (action: UIAlertAction!) in
// I have code here
}))
alertView.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action: UIAlertAction!) in
// I have code here
}))
newAlert = alertView
presentViewController(alertView, animated: true, completion: nil)
}
// When I need to close. If blah is below 80
if blah < 80 {
if let newAlertView = newAlert as? UIAlertController {
newAlertView.dismissViewControllerAnimated(true, completion: nil)
}
}
Upvotes: 0