NavigatorController Button not displaying popup alert - swift

I was creating a simple Navigationcontroller Button to show an alert to click but it fails to do Please find the code below:
//Enable the buttons to edit
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Edit", style: .plain, target: self, action: #selector(EditEnable(sender:)))
#objc private func EditEnable(sender: UIBarButtonItem)
{
let alert = UIAlertController(title: "Time to change the Menu for Next Week",
message: "Message",
preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default))
}
Not sure what I am doing is wrong. Please help.

You're forgetting to show the alert.
self.present(alert, 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)

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

Making a privacy policy in Swift: How to make UIAlertController action button disabled until message is scrolled to the bottom

In my iOS application I am trying to implement an easy privacy policy using a UIAlertController. By law, the policy has to be scrollable before it can be accepted - like most privacy policies these days.
From my own research I have seen that you can disable and enable UIAlertAction buttons but I don't know how to identify when the UIAlertController message body has been scrolled. Scrolling all the way to the bottom may be a requirement and I am interested in figuring out a way that would work too.
Here is my current code for the default looking UIAlertController above.
let alertController = UIAlertController(title: "Privacy Policy", message: privacyPolicyString, preferredStyle: UIAlertControllerStyle.Alert)
let AcceptAction = UIAlertAction(title: "Accept", style: UIAlertActionStyle.Default, handler: {(action: UIAlertAction) -> Void in
//perform next step in login verification
})
let DeclineAction = UIAlertAction(title: "Decline", style: UIAlertActionStyle.Default, handler: {(action: UIAlertAction) -> Void in
//User has declined privacy policy. The view resets to standard login state
})
alertController.addAction(AcceptAction)
alertController.addAction(DeclineAction)
self.presentViewController(alertController, animated: true, completion: nil)
This can be possible with native UIAlertController as follows:-
Create variable of type UIAlertAction
Set enabled to false
When you scrolled to bottom, you can check this on scroll view delegate or any custom way, then set the enabled to true
The most important thing in below code is:-
self.actionToEnable = action
action.enabled = false
Code reference:-
weak var actionToEnable : UIAlertAction?
func callbackWhenScrollToBottom(sender:UIScrollView) {
self.actionToEnable?.enabled = true
}
let alert = UIAlertController(title: "Title", message: "Long text here", preferredStyle: UIAlertControllerStyle.Alert)
let cancel = UIAlertAction(title: "Accept", style: UIAlertActionStyle.Cancel, handler: { (_) -> Void in
})
let action = UIAlertAction(title: "Decline", style: UIAlertActionStyle.Default, handler: { (_) -> Void in
})
alert.addAction(cancel)
alert.addAction(action)
self.actionToEnable = action
action.enabled = false
self.presentViewController(alert, animated: true, completion: nil)

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)

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
}