So for a bit now i have been trying to figure this out with no luck. What i am trying to do goes like this...
- Action sheet pops up and option is tapped which brings up an alert box
- Textfield in alert box asks to name a deck
- Name is entered,"OK" is tapped and segue brings user to next screen.
- If textfield is empty, pop up another alert box telling the user to enter a valid name.
Now i wanted to check if that textfield within the alert box was empty or not, so what i did was added a check variable in my showAlertTapped function in order to check if the textfield was empty or not. Used an if statement to either segue, or pop up the second alert box as well. My issue is the second alert box is popping up asking to enter a valid name whether the textfield is empty or not. I tried changing some things around, such as the initial bool check variable and also switching textField.text == "" to textField.text == nil, but all that did was segue both times with no alert box showing. A couple other things that i tried that have slipped my mind, but the end result was always the same, either segueing both times, or second alert box both times. So i am stuck. I am going to post the code to make it easier to see if i am totally missing something, and i apologize in advance if my formatting is off for this post, or if my code is not as neat as it should be. I am still a beginner and learning as i go. So if i have something sloppy, or formatted wrong, i would be willing to fix it. Thank you for your time.
func showAlertTapped() {
var check = true
//Create the AlertController
let firstAlert: UIAlertController = UIAlertController(title: "New questions deck", message: "Enter a name for your questions deck", preferredStyle: .Alert)
//Create and add the Cancel action
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
}
firstAlert.addAction(cancelAction)
let saveAction: UIAlertAction = UIAlertAction(title: "Save", style: .Default) { action -> Void in
if check == true {
self.shouldPerformSegueWithIdentifier("saveSegue",sender: self)
}
else{
let alert: UIAlertController = UIAlertController(title: nil, message: "Please enter a valid name", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in
self.dismissViewControllerAnimated(true, completion: nil)
}))
//Present the AlertController
self.presentViewController(alert, animated: true, completion: nil)
}
}
firstAlert.addAction(saveAction)
//Add a text field
firstAlert.addTextFieldWithConfigurationHandler { textField -> Void in
if textField.text == "" {
check = false
}
textField.textColor = UIColor.blueColor()
}
//Present the AlertController
self.presentViewController(firstAlert, animated: true, completion: nil)
}