0

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...

  1. Action sheet pops up and option is tapped which brings up an alert box
  2. Textfield in alert box asks to name a deck
  3. Name is entered,"OK" is tapped and segue brings user to next screen.
  4. 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)

}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
ACerts
  • 308
  • 1
  • 6
  • 22
  • 1
    Rather than answer your question I am going to suggest a change to your UI flow - popping a modal alert is annoying - the user has to tap to acknowledge it and then since you can't prevent the UIAlertController from being dismissed it will then have to be redisplayed . Although it is a bit more work you should create your own view controller to manage a modal dialog that collects the name; this gives you much better control over the user experience and you can simply disable the 'OK' button until the text field has content using the text field delegate – Paulw11 Nov 01 '15 at 03:05

1 Answers1

0

Regardless of the design, to just make your code work:

The problem:

 if textField.text == "" {
        check = false
 }

This code won't work the way as you expected, the block will be called before it is displayed, not be called when the textfield changed. see this document.

So to fix it, you can just check whether the textfield is empty when user click the save button:

 let saveAction: UIAlertAction = UIAlertAction(title: "Save", style: .Default) { action -> Void in
    let text = (firstAlert.textFields?[0] as UITextField).text
    if text != nil && text!.characters.count>0 {
        self.performSegueWithIdentifier("saveSegue",sender: self)
    }
   ....other codes....
 }

I have not test the code, hopefully it will work. Post a comment if there is any error.

Surely
  • 1,649
  • 16
  • 23
  • Hi zp_x, Thank you for your reply. So i followed the code and when the textfield is empty, it pops up the alert box as i wanted, but when i add some characters in there and hit "Save", nothing happens. Segue doesnt go. Any suggestions? – ACerts Nov 01 '15 at 23:24
  • Got it to work. i switched out shouldPerformSegueWithIdentifier with performSegueWithIdentifier. Is this a proper thing to do? – ACerts Nov 02 '15 at 22:40
  • @aCertos ya, you are right, shouldPerformSegueWithIdentifier is called before doing the transition. – Surely Nov 03 '15 at 02:31