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

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)

Related

How to execute a block of code right after UIAlertController was dismissed

Is there a way to execute a block of code on dismissing UIAlertController without subclassing it or involving UIAlertActions?
I need to safely dispose an observable binded to alert's textfiled on its dismissal.
I think that you must to use the alert action, but if you want, you can mix with completion, like this
func showAlertMessageCompletion(titleStr:String, messageStr:String, completion: #escaping ((Bool) -> Void)) {
let alert = UIAlertController(title: titleStr, message: messageStr, preferredStyle: UIAlertController.Style.alert)
let action = UIAlertAction(title: "Aceptar", style: .default) { (_) in
completion(true)
}
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}
You need to add handler to alert action button.
let alertController = UIAlertController(title: "Alert title", message: "Message to display", preferredStyle: .alert)
// Create OK button
let OKAction = UIAlertAction(title: "OK", style: .default) { (action:UIAlertAction!) in
// Code in this block will trigger when OK button tapped.
print("Ok button tapped");
}
alertController.addAction(OKAction)
// Create Cancel button
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action:UIAlertAction!) in
print("Cancel button tapped");
}
alertController.addAction(cancelAction)
// Present Dialog message
self.present(alertController, animated: true, completion:nil)

How to place my App Icon into an iOS alert window

I want to place my app icon into an alert controller above the alertTitle - same as for the alert that opens if an app review is called by SKStoreReviewController.requestReview().
I have not found any documentation about it.
So, what to do with the code below to let the App Icon appear?
let alertController = UIAlertController(title: alertHeaderText, message: alertText, preferredStyle: UIAlertController.Style.alert)
let option1: UIAlertAction = UIAlertAction(title: "text", style: .default, handler: {
(action) in
// some code
})
let option2: UIAlertAction = UIAlertAction(title: "text", style: .default, handler: {
(action) in
// some code
})
alertController.addAction(option1)
alertController.addAction(option2)
self.present(alertController, animated: true, completion: nil)

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

UIAlertController --> Used to update variable?

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
}