how to display alert controller only once when the UITextfield is tapped? - swift

I'm trying to prevent user from manipulating with their data, so when they tap on UITextfield an alertController shall be displayed only once, after they press "yes" option, then they shall be able to change data. but instead of displaying alert controller once, it keeps displaying every time I tap on UITextfield; how I can solve this issue.
func textFieldDidBeginEditing(textField: UITextField) {
let alert = UIAlertController(title: "Warning !", message: "Are your sure you want to change your content? ", preferredStyle: .Alert)
let yes = UIAlertAction(title: "Yes", style: .Cancel) { (action) in
}
alert.addAction(yes)
let no = UIAlertAction(title: "No", style: .Default) { (action) in
}
alert.addAction(no)
presentViewController(alert, animated: true) {}
}
textField.becomeFirstResponder()
}

var alertWillShow = true
func textFieldDidBeginEditing(textField: UITextField) {
if alertWillShow {
alertWillShow = false
let alert = UIAlertController(title: "Warning !", message: "Are your sure you want to change your content? ", preferredStyle: .Alert)
let yes = UIAlertAction(title: "Yes", style: .Cancel) { (action) in
}
alert.addAction(yes)
let no = UIAlertAction(title: "No", style: .Default) { (action) in
}
alert.addAction(no)
presentViewController(alert, animated: true) {}
}
}
textField.becomeFirstResponder()
}

Related

How to store text of alertView in Firebase Firestore?

I was hoping someone could help me with storing the data inputted into my alertView textField to firebase firestore. I have a collection called "HelpRequests" and I want the information to be stored in that collection. Below is the code for the alert.
#objc func alertTapped(sender:UIButton!){
let alert = UIAlertController(title: "Report an issue", message: "Please allow 24hour to respond", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Done", style: .default) { (action) in
if let te = alert.textFields?.first?.text , te.count > 0 {
}
})
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel) { (action) in
// Respond to user selection of the action.
})
alert.addTextField { (textField) in
textField.placeholder = ""
}
self.present(alert, animated: true)
}

Swift alert not performing action

I have an application that is showing an alert for incomplete input as shown:
if (foodChosen == "") {
let alertAction = UIAlertController(title: "Continue?", message: "No food selected. Do you wish to continue?", preferredStyle: .alert)
let yes = UIAlertAction(title: "Yes", style: .default, handler: { (UIAlertAction) in
self.finish = true
})
alertAction.addAction(yes)
let no = UIAlertAction(title: "No", style: .default, handler: { (UIAlertAction) in
})
alertAction.addAction(no)
self.present(alertAction, animated: true, completion: nil)
} else {
self.finish = true
}
where self.finish is initialised to false at the beginning of the view controller.
Even when selecting the "Yes" button, the value of self.finish is never updated. Can anyone see why?

Swift4 - Multiple Alerts with Text Field - Completion Handler

i am working at my startscreen (viewDidAppear). At the beginning of the app, there should be an alert with some notice message. This works fine. After you click "ok" at the notice, the next alert should pop up with a text field. In this text field you have to type in a double value. I need this double value to set up a lot of things inside the app, so I need this value outside the function to calculate with it.
E.g. The notice message describes how the app works. You click OK. Now the next alert pop up with the text field. For example it asks your height. After you type in your height and click OK the height-value is the main part of different calculations.
The problem is now, that my app works with the nil-value before I typed in the double value (e.g. height)
These are the snippets from the code.
var iNeedTheDataHere:String?
///Alerts
//Start
let startController = UIAlertController(title: "Notice", message: "notice message ", preferredStyle: .alert)
let inputController = UIAlertController(title: "Set input", message: "Please set the input", preferredStyle: .alert)
//BeforeScreenLoad
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// Start Alert
startController.addAction(UIAlertAction(title: "OK", style: .default, handler:{(action:UIAlertAction!) in
self.input()
}))
self.present(startController, animated: true)
}
func input() {
inputController.addTextField()
let submitAction = UIAlertAction(title: "Submit", style: .default) { [unowned ac] _ in
let answer = inputController.textFields![0].text
iNeedTheDataHere = answer
}
inputController.addAction(submitAction)
}
When I set Breakpoints, I see that the value of the inputController is set to nil, before I typed in anything. I guess my mistake has something to do with the handler
Sorry for my English
Hope somebody can help me
let alert = UIAlertController(title: "Alert!!!", message: "Please enter your message ", preferredStyle: .alert)
alert.addTextField(configurationHandler: self.configurationTextField)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler:{ (UIAlertAction) in
self.number.text?=""
MBProgressHUD.hide(for: self.view, animated: true)
}))
alert.addAction(UIAlertAction(title: "Done", style: .default, handler:{ (UIAlertAction) in
}))
self.present(alert, animated: true, completion: {
})
I have made some changes in your code to achieve that:
class ViewController: UIViewController {
let startController = UIAlertController(title: "Notice", message: "notice message ", preferredStyle: .alert)
let inputController = UIAlertController(title: "Set input", message: "Please set the input", preferredStyle: .alert)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(_ animated: Bool) {
startController.addAction(UIAlertAction(title: "OK", style: .default, handler:{(action:UIAlertAction!) in
self.input()
}))
self.present(startController, animated: true)
}
func input() {
//Present another alert with textField
let confirmAction = UIAlertAction(title: "Submit", style: .default) { (_) in
guard let textFields = self.inputController.textFields,
textFields.count > 0 else {
// Could not find textfield
return
}
//get your textField
let answerTF = textFields[0]
let answer = answerTF.text
//Get values entered by user
print(answer)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in }
//add your textField here
inputController.addTextField { (textField) in
//Set a placeholde for textField
textField.placeholder = "Answer"
}
inputController.addAction(confirmAction)
inputController.addAction(cancelAction)
self.present(inputController, animated: true, completion: nil)
}
}
I have added comment to explain what I am doing.
And your result will be:
You can also check demo project here.

my segue is not working

I have a button that if user click on it it must show another vc and it's working properly until I add UIAlertController which it's getting data from user and after user click done on alertaction nothing happens
this is my code:
#IBAction func DfsClicked(_ sender: AnyObject) {
let alertController = UIAlertController(title: "DFS?", message: "Please input DFS depth:", preferredStyle: .alert)
let confirmAction = UIAlertAction(title: "Confirm", style: .default) { (_) in
if let field = alertController.textFields?[0] {
// store your data
self.depth = Int(field.text!)!
print(self.depth)
Puzzle.AnswerNode = dfs(inputdepth: self.depth,SortedPuzzle:self.SortedPuzzle)
} else {
// user did not fill field
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in }
alertController.addTextField { (textField) in
textField.placeholder = "Default is 2"
textField.keyboardType = UIKeyboardType.numberPad
}
alertController.addAction(confirmAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true)
}
Use this method to perform segue, inside confirm action block
self.performSegue(withIdentifier: <Identifier>, sender: <Any?>)

Swift shouldSelectViewController using alertController

I have what seems like a normal use case: on selecting a certain tab ('logout' in my case), I want to display an alert asking for confirmation before transitioning. This seems like it should be handled in the shouldSelectViewController function. However, the alertController is async, and I'm forced to return a boolean before it completes. It doesn't seem likely that shouldSelectViewController should wait for an async task before completing. Is there a better place for me to trigger my confirmation alert before switching views?
This is my code:
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
if (viewController is Trove.HomeViewController) {
var shouldSelect = false
let alertController = UIAlertController(title: "Logout", message: "Are you sure you want to log out?", preferredStyle: .Alert)
let okAction = UIAlertAction(title: "Logout", style: .Default) { (action) in
shouldSelect = true
}
alertController.addAction(okAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in }
alertController.addAction(cancelAction)
tabBarController.presentViewController(alertController, animated: true) { return shouldSelect}
}
return true
}
Any help is much appreciated! Thanks!
You can try setting the selecedIndex property of the UITabBarController.
Your new if statement:
if (viewController is Trove.HomeViewController) {
let alertController = UIAlertController(title: "Logout", message: "Are you sure you want to log out?", preferredStyle: .Alert)
let okAction = UIAlertAction(title: "Logout", style: .Default) { (action) in
tabBarController.selectedIndex = 0 //CHANGE ME
}
alertController.addAction(okAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in }
alertController.addAction(cancelAction)
tabBarController.presentViewController(alertController, animated: true)
return false
}