UIAlertController Keeps Re-Appearing After Closing It - ios

I have written code for an alert to appear when the input in one of my UITextFields is less than 1050. It successfully appears when the inputs satisfies that, but after I press "OK" it instantly re-appears.
Below is the code in the viewDidLoad function:
override func viewDidLoad(){
super.viewDidLoad()
alert = UIAlertController(title: "Error", message: "Please enter an exit width value greater than 1050", preferredStyle: UIAlertControllerStyle.Alert)
let okay = UIAlertAction(title: "OK", style: UIAlertActionStyle.Destructive, handler: valueCalc)
alert.addAction(okay)
}
Then I have in my valueCalc function (which is called when a button is tapped):
#IBAction func valueCalc(sender: AnyObject){
if(Int(mmText.text!)! < 1050){ //mmText is an UITextField
self.presentViewController(alert, animated: true, completion: nil)
}
}

According to your line of code
let okay = UIAlertAction(title: "OK", style: UIAlertActionStyle.Destructive, handler: valueCalc)
Your handler name valueCalc is called when you press OK.
Again the value is calculated which when come out be less then the specified characters shows back you the alert.
Instead of that, replace this line in your code -
let okay = UIAlertAction(title: "OK", style: UIAlertActionStyle.Destructive, handler: handlerMethod)
and add this method to your code
func handlerMethod() {
//handle your action here after ok is pressed for e.g if you wanna just dismiss the alert then write
dismissViewControllerAnimated(true, completion: nil)
}

You have the handler argument for your UIAlertAction set to valueCalc. Therefore, whenever the user taps "OK", the method valueCalc gets run again, and since the value is (presumably) still the same, the alert is presented right back again.

Try this
override func viewDidLoad(){
super.viewDidLoad()
alert = UIAlertController(title: "Error", message: "Please enter an exit width value greater than 1050", preferredStyle: UIAlertControllerStyle.Alert)
let okay = UIAlertAction(
title: "OK",
style: UIAlertActionStyle.Destructive) { (action) in }
}
#IBAction func valueCalc(sender: AnyObject){
if(Int(mmText.text!)! < 1050){ //mmText is an UITextField
self.presentViewController(alert, animated: true, completion: nil)
}

Related

How to pass multiple handlers in UIAlertAction

I've few buttons, clicking on these buttons present same alert. I've to handle each button click separately so I need to pass different handlers to each of these alerts. How do I do it?
I've searched for solutions but I can't find what I'm looking for, or may be It's there but I'm not able to understand it.
Following is my code snippet. Inside this function I can get which button is clicked but I'm not able to figure out how to call different handlers and pass them alert.title.
I hope someone can point me in right direction.
#IBAction func buttonClicked(_ sender: UIButton) {
let alert = UIAlertController(title: "Select Value", message: nil, preferredStyle: .actionSheet)
for list in self.listValue {
alert.addAction(UIAlertAction(title: list.value, style: .default, handler: { _ in
// How do I call different handlers here?
// I'll need to retrieve alert.title in these handlers
}))
}
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: nil))
self.present(alert, animated: false, completion: nil)
}
It's not totally clear what you're asking, but if you are trying to figure out which button was pressed so that you can execute different methods for each one you can do something like this:
#IBAction func buttonClicked(_ sender: UIButton) {
let alert = UIAlertController(title: "Select Value", message: nil, preferredStyle: .actionSheet)
for list in self.listValue {
alert.addAction(UIAlertAction(title: list.value, style: .default, handler: { (action) in
// How do I call different handlers here?
// I'll need to retrieve alert.title in these handlers
switch action.title {
case "Value A":
print("It's Value A")
case "Value B":
print("It's Value B")
default:
print("We didn't implement anything for this value")
}
}))
}
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: nil))
self.present(alert, animated: false, completion: nil)
}

Swift: How to make an action occur if UIAlertAction is pressed?

I'm trying to make an app where if the score is 3 the app displays a message that says "you lose" but keeps '3' as the number in the score label until the End Game option in the popup is pressed, at which point the score goes back to 0 for a new game. I am new to swift and am having difficulty and would really appreciate any and all help! I am not sure if making an IBAction for the alert action is the right thing to do or not.
else if rightscorecount == 3 {
let alert = UIAlertController(title: "Game", message: "You Lose!", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "End Game", style: UIAlertActionStyle.default) { UIAlertAction in})
self.present(alert, animated: true, completion: nil)
}
}
#IBAction func test(sender: UIAlertAction) {
rightscorecount = 0
rightscorelabel.text = String(rightscorecount)
}
Try this:
let alertController = UIAlertController.init(title: "Game", message: "You Lose!", preferredStyle: .alert)
alertController.addAction(UIAlertAction.init(title: "End Game", style: .default, handler: { (action) in
// Your handler goes here
self.someFunction()
}))
self.present(alertController, animated: true) {
// Completion block
}
And your function
func someFunction() {
// Function body goes here
}

Swift: UITextField is not being recognized

I want the user to press a button, and then for them to be able to see an alert where they can enter an input (to set a price for a service). The other logic involves saving data to a database, which is not really relevant to my problem.
I am using the following example:
https://stackoverflow.com/a/30139623/2411290
It definitely works, in that it shows the alert correctly, but once I include
print("Amount: \(self.tField.text)")
"self.tField.text" is not recognized. The specific error I get is:
value of type 'testVC' has no member 'tField'
#IBAction func setAmount(_ sender: Any) {
var tField: UITextField!
func configurationTextField(textField: UITextField!)
{
print("generating textField")
textField.placeholder = "Enter amount"
tField = textField
}
func handleCancel(alertView: UIAlertAction!)
{
print("Cancelled")
}
let alert = UIAlertController(title: "Set price of service", message: "", preferredStyle: .alert)
alert.addTextField(configurationHandler: configurationTextField)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler:handleCancel))
alert.addAction(UIAlertAction(title: "Done", style: .default, handler:{ (UIAlertAction) in
print("Done !!")
}))
self.present(alert, animated: true, completion: {
print("completion block")
print("Amount: \(self.tField.text)") // Error here
})
//// other logic for app
}
tField is a local variable inside your setAmount function. It is not a property of the class.
Change:
self.tField.text
to:
tField.text
That will allow you to access the local variable.
But the real question is why are you creating a local variable of UITextField inside this function? Why are you printing its text when the text field isn't used anywhere?
Most likely you should be accessing the alert's text field inside the action handler for the "Done" button. There's no need to do anything inside the completion block of presenting the alert.
#IBAction func setAmount(_ sender: Any) {
let alert = UIAlertController(title: "Set price of service", message: "", preferredStyle: .alert)
alert.addTextField(configurationHandler: { (textField) in
print("generating textField")
textField.placeholder = "Enter amount"
})
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel) { (action) in
print("Cancelled")
})
alert.addAction(UIAlertAction(title: "Done", style: .default) { (action) in
print("Done !!")
if let textField = alert.textFields?.first {
print("Amount: \(textField.text)")
}
})
self.present(alert, animated: true, completion: nil)
}
My guess is that when you present an alert your current ViewController is the alert viewController... And in your alert there is no variable tField.
On the exampled you quoted the alert was presented only after the print with tField's value. That why that worked there and doesn't work in your case.

Reload View after UIAlertController

I have a signup screen with a phone number field.
If the user type a too short phone number, I display an UIAlertController.
After she dismisses the Alert, the user can't send her phone number again as the send button is tapped.
Is there a way to reload the view when the alert is dismissed to make the button untapped?
Thanks
func present_alert(title:String, content : String){
let myAlert = UIAlertController(title:title, message:content, preferredStyle:UIAlertControllerStyle.alert)
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler:nil)
myAlert.addAction(okAction)
self.present(myAlert, animated:true)
}
the main reason on button click you disabled the yourbutton sender.isEnabled = false, when ever the alert is presented again enable your button , surely works addyourbuttonName.isEnabled = true
if you want to clear the current textfield value then use yourtextfield.text = ""
let alertController = UIAlertController(title: “Simple”, message: “Simple alertView demo with Cancel and Ok.”, preferredStyle: UIAlertControllerStyle.alert)
let okAction = UIAlertAction(title: “OK”, style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in
print(“OK”)
yourtextfield.text = ""
yourtextfield.becomeFirstResponder()
addyourbuttonName.isEnabled = true
}
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
func present_alert(title:String, content:String, button:UIButton){
let myAlert = UIAlertController(title:title, message:content, preferredStyle:UIAlertControllerStyle.alert)
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler:nil)
myAlert.addAction(okAction)
self.present(myAlert, animated:true){
button.isEnabled = true
}
}
As i understand your issue the problem is that you receive your button false value of parameter "isEnabled". To fix that just add to your block last string:
buttonName.isEnabled = true
After click on UIAlertAction(cancel or ok), you can add some functionality in that ok or cancel method. In the final step of that method, add viewWillAppear(true) method. Then UIViewController automatically refresh.

How to disable a UIButton and send a UIAlertView message

I am trying to disable a menu button is the array is shows is empty.
This is my code.
#IBAction func likedmenubuttontouched(sender: AnyObject) {
if Globals.likedArray.isEmpty {
likedMenuButton.userInteractionEnabled = false
let ac = UIAlertController(title: "No liked quotes yet", message: "No liked quotes have been chosen, go explore!", preferredStyle: .Alert)
ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
presentViewController(ac, animated: true, completion: nil)
return
} else {
likedMenuButton.userInteractionEnabled = true
}
}
And in ViewDidLoad()
likedMenuButton.userInteractionEnabled = false
I have managed to disable the button, but I want to send a message alerting the user why the button is disabled, otherwise, its a little confusing.
How would I go about doing this?
Thanks.
As by default the the user Interaction is true you need not to make it true
#IBAction func likedmenubuttontouched(sender: AnyObject) {
if Globals.likedArray.isEmpty {
let ac = UIAlertController(title: "No liked quotes yet", message: "No liked quotes have been chosen, go explore!", preferredStyle: .Alert)
ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
presentViewController(ac, animated: true, completion: nil)
} else {
//segue to the other view
}
}
Also check your array count, that is why your wrong condition is executing

Resources