UIAlertController --> Used to update variable? - swift

I'm new to Xcode and Swift. I'm trying to set up an alert (which I have done - code below). I was wondering if someone can help me out. What I would like to do is reset two different variables to 0 if "Yes" is tapped.
Your help is greatly apprecieated.
#IBAction func showAlert() {
let alertController = UIAlertController(title: "Confirm Point Reset", message: "Are You Sure?", preferredStyle: .Alert)
let firstAction = UIAlertAction(title: "Yes", style: .Default, handler: nil)
let secondAction = UIAlertAction(title: "No", style: .Default, handler: nil)
alertController.addAction(firstAction)
alertController.addAction(secondAction)
presentViewController(alertController, animated: true, completion: nil)
}

You need to add the completion handler and update the values in that closure. The completion handler is the code that is executed when the action occurs (when the button is pressed).
let firstAction = UIAlertAction(title: "Yes", style: .Default) { action in
self.foo = 0
}

Related

UIAlertController - Order of actions [duplicate]

This question already has answers here:
Unable to choose order of buttons in UIAlertController
(2 answers)
Closed 5 years ago.
I wanna show alert window by pressing on button. Usual thing. But I'm confused that I try to show button "Awesome" at first, but always "Cancel" button stay the first. How can I fix it?
let alert = UIAlertController(title: "Hello world", message: "Testing", preferredStyle: .alert)
let action = UIAlertAction(title: "Awesome", style: .default){(_) in print("Awesome")}
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alert.addTextField(configurationHandler: nil)
alert.addAction(action)
alert.addAction(cancel)
alert.actions.forEach( { (action) in print( action.title! ) } )
present(alert, animated: true, completion: nil)
Irregular order of buttons
Changing the order will not work as default position of cancel button is left.
Change the action style for cancel button type to UIAlertActionStyleDefault instead of UIAlertActionStyleCancel.
Try like this, it will work for you.
let alertController = UIAlertController(title: "", message: "", preferredStyle: UIAlertControllerStyle.alert)
let destructiveAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default) {
(result : UIAlertAction) -> Void in
}
let okAction = UIAlertAction(title: "Awesome", style: UIAlertActionStyle.default) {
(result : UIAlertAction) -> Void in
}
alertController.addAction(destructiveAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)

Change the color of cancel button in UIAlertController with preferredStyle: .ActionSheet

Is it possible to change the color of cancel button to red , i know we can by using Destructive style
let cancelActionButton: UIAlertAction = UIAlertAction(title: "Cancel", style: .Destructive) { action -> Void in
print("Cancel")
}
but i want the cancel button separately , like this
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
cancelAction.setValue(UIColor.red, forKey: "titleTextColor")
This is code of how to make the alert like you said:
let alert = UIAlertController(title: "Hello", message: "Hello World", preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Open in Google Maps", style: . default, handler: nil))
alert.addAction(UIAlertAction(title: "Open in Google", style: . default, handler: nil))
alert.addAction(UIAlertAction(title: "Copy Address", style: . default, handler: nil))
alert.addAction(UIAlertAction(title: "Cancel", style: .destructive, handler: nil))
You have to use 2 kind of style.
In here, I used .destructive and .default, It will separate alert action into 2 part
Swift 4
You can change the color of the alert action button using the below code.
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
cancelAction.setValue(UIColor.red, forKey: "titleTextColor")
Hope this helps you.
Just give the style property of the button as destructive.
let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: {
(alert: UIAlertAction!) -> Void in
})
Swift4.2
If you have multiple UIAlertAction, then add "Cancel" UIAlertAction in UIAlertController like that.
let alert = UIAlertController(title: "Title", message: "Your Message", preferredStyle: UIAlertController.Style.actionSheet)
alert.addAction(UIAlertAction(title: "first",style: .default, handler: { action in
//Do something....
}))
alert.addAction(UIAlertAction(title: "second", style: .default, handler: { action in
//Do something....
}))
// Add cancel UIAlertAction
let cancelAlert = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
cancelAlert.setValue(UIColor.red, forKey: "titleTextColor")
alert.addAction(cancelAction).
self.present(alert, animated: true, completion: nil)
If you want to achieve the same output for the cancel button and also don't want to change the cancel button type to destructive. I have used cancel type for the cancel button in the code. To achieve the same, You can use the following code:-
//MARK:- Function to create the action sheet
func showAlertSheet(){
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
// Create Google Map button
let googleMap = UIAlertAction(title: "Open in Google Maps", style: .default) { (action:UIAlertAction!) in
// Code in this block will trigger when OK button tapped.
print("Ok button tapped");
}
alertController.addAction(googleMap)
// Create Map button
let map = UIAlertAction(title: "Open in Maps", style: .default) { (action:UIAlertAction!) in
// Code in this block will trigger when OK button tapped.
print("Ok button tapped");
}
alertController.addAction(map)
// Create copy Address button
let copyAddress = UIAlertAction(title: "Copy Address", style: .default) { (action:UIAlertAction!) in
// Code in this block will trigger when OK button tapped.
print("Ok button tapped");
}
alertController.addAction(copyAddress)
// Create Cancel button
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action:UIAlertAction!) in
print("Cancel button tapped");
}
// Change Cancel title color according to your requirements
cancelAction.setValue(UIColor.red, forKey: "titleTextColor")
alertController.addAction(cancelAction)
// Present Dialog message
self.present(alertController, animated: true, completion:nil)
}
And also you have the option to change the cancel button text color. The output of the code is like this:-
Change the style from UIAlertActionStyleDefault to UIAlertActionStyleDestructive in objective C:
UIAlertAction* button = [UIAlertAction actionWithTitle:#"Button title here"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction * action)
{
// Handle action here....
}];
You can use alert.view.tintColor which will be applied for .cancel and .default styles

View controller navigation / transition through POP UP action button (UIAlertAction)

Following are the lines of code that I am using in swift (Xcode) for creating a pop up.
//create the alert
let alert=UIAlertController(title: "Better Luck Next Time", message: "Try Again Later", preferredStyle: UIAlertControllerStyle.Alert)
// add an action (button)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
// show the alert
self.presentViewController(alert,
animated: true,
completion: nil)
Once the user presses the OK button in the pop up menu; I want the app to navigate to another view controller. I know that I have to put some lines of code in the handler part of UIAlertAction. But I am not sure of how to code this transition. Any one has any simple and effective ideas ??.
let alertController = UIAlertController(title: "Default AlertController", message: "A standard alert", preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action:UIAlertAction!) in
println("you have pressed the Cancel button");
}
alertController.addAction(cancelAction)
let OKAction = UIAlertAction(title: "OK", style: .Default) { (action:UIAlertAction!) in
println("you have pressed OK button");
if let navController = self.navigationController
{
navController.popViewControllerAnimated(true)
}
}
alertController.addAction(OKAction)
self.presentViewController(alertController, animated: true, completion:nil)

Cannot call value of non-function type UIAlertAction

I'm not sure what's wrong with this function. I'm trying to present an alert asking if the user would like to delete the selected photo.
If the function that deletes the photo returns an error, I would like to show that error to the user.
Xcode is failing on the errorController.addAction line with the message that "cannot call value of non-function type UIAlertAction"
I'm using Swift 2
#IBAction func deletePhoto(sender: AnyObject) {
let alertController = UIAlertController(title: "Delete Photo", message: "Are you sure you want to delete this photo?", preferredStyle: .Alert)
let okAction = UIAlertAction(title: "OK", style: .Default) { UIAlertAction in
self.photoGateway!.delete(self.photo!.id!, completion: { (withError: Bool) -> Void in
if (withError == true) {
let errorController = UIAlertController(title: "Delete Failed", message: "blah", preferredStyle: UIAlertControllerStyle.Alert)
// The next line is causing the error
errorController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(errorController, animated: true, completion: nil)
} else {
self.dismissViewControllerAnimated(true, completion: nil)
}
})
}
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { UIAlertAction in
print("Cancelled")
}
alertController.addAction(okAction)
alertController.addAction(cancelAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
If I take out the offending line then all works well, just the user has no way of dismissing the alert
Fixed it. Changed:
errorController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
to:
errorController.addAction(UIKit.UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
The reason this is happening is because the parameter for your callbacks is called UIAlertAction (lines 3 and 20 above) and this is overriding the declaration in UIKit. This is likely a mistake of code-completion. Just rename it to action or something like that or just _ as you don't reference it.

Can a UIAlertAction trigger a function?

Is it possible to connect a function to an UIAlertAction?
So after a user clicks the OK button it performs an action? Isn't this what the handler parameter does?
let alert: UIAlertController = UIAlertController(title: "Email already registered", message: "Please enter a different email", preferredStyle: .alert)
let okButton = UIAlertAction(title: "OK", style: .default, handler: backToLogin())
alert.addAction(okButton)
self.presentViewController(alert, animated: true, completion: nil)
...
func backToLogin() {
self.performSegueWithIdentifier("toLoginPage", sender: self)
}
You can use a function as the handler, but it needs to have the correct type. Also you must not call it when you pass it as an argument, i.e., instead of handler: backToLogin() (which would set the return value of backToLogin as the handler) you would have handler: backToLogin without the ().
The following should work:
func backToLogin(alertAction: UIAlertAction) {
self.performSegueWithIdentifier("toLoginPage", sender: self)
}
let okButton = UIAlertAction(title: "OK", style: .Default, handler: backToLogin)
But having to change backToLogin might defeat the purpose, so you could just use a closure:
let okButton = UIAlertAction(title: "OK", style: .Default) { _ in
self.backToLogin()
}
You need to enter the handler
let okButton = UIAlertAction(title: "OK", style: .Default, handler: {
(UIAlertAction) in
self.backToLogin()
})
}
See this answer for more info: Writing handler for UIAlertAction