Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Alexander Certosimo
1,774 PointsChecking for nil in textfield of Alert box
Hey all,
So i have an action sheet that then brings up an alert box. From that alert box when "save" is tapped, i want to segue to another view. I had this working with performSegueWithIdentifier() , but i want to make sure the textfield is not empty first, and if it is, pop up another alert box reminding the user to input something in the textfield. Any help with this? Thanks in advance
@IBAction func showActionSheet(sender: AnyObject) {
let actionSheetController: UIAlertController = UIAlertController(title: nil, message: "What would you like to do?", preferredStyle: .ActionSheet)
//Create and add the Cancel action
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
}
actionSheetController.addAction(cancelAction)
//Create and add first option action
let createNewDeckAction: UIAlertAction = UIAlertAction(title: "Create new question deck", style: .Default) { action -> Void in self.showAlertTapped()
}
actionSheetController.addAction(createNewDeckAction)
self.presentViewController(actionSheetController, animated: true, completion: nil)
}
func showAlertTapped() {
//Create the AlertController
let actionSheetController: 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
}
actionSheetController.addAction(cancelAction)
//Create and an option action
let saveAction: UIAlertAction = UIAlertAction(title: "Save", style: .Default) { action -> Void in
//self.shouldPerformSegueWithIdentifier("saveSegue", sender: self)
}
actionSheetController.addAction(saveAction)
//Add a text field
actionSheetController.addTextFieldWithConfigurationHandler { textField -> Void in
textField.textColor = UIColor.blueColor()
}
//Present the AlertController
self.presentViewController(actionSheetController, animated: true, completion: nil)
}
1 Answer
Jhoan Arango
14,575 PointsPerhaps you can do something like this, on your showAlertTapped() function.
func showAlertTapped() {
var check = true
//Create the AlertController
let actionSheetController: 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
}
actionSheetController.addAction(cancelAction)
//Create and an option action
let saveAction: UIAlertAction = UIAlertAction(title: "Save", style: .Default) { action -> Void in
if check == false {
//self.shouldPerformSegueWithIdentifier("saveSegue", sender: self)
}
else {
// Make another alert
}
}
actionSheetController.addAction(saveAction)
//Add a text field
actionSheetController.addTextFieldWithConfigurationHandler { textField -> Void in
textField.textColor = UIColor.blueColor()
if textField.text != "" {
check = false
}
}
//Present the AlertController
self.presentViewController(actionSheetController, animated: true, completion: nil)
}
Jhoan Arango
14,575 PointsJhoan Arango
14,575 PointsIf not, then you would have to conform to the UITextFieldDelegate, and use delegation to know when a user is typing something in the textField.
Let me know if this works. Or if I can help more.
Alexander Certosimo
1,774 PointsAlexander Certosimo
1,774 PointsHey Jhoan,
Thanks for the quick help. So i thought i had it working, at first it wasnt doing anything for me, not popping up the second alert box, nor segueing to the next view. Then i remembered a past discussion we had where changing nil to "" worked. I tried that, but then it would ONLY pop up the second alert box no matter if the textfield was empty or not.
Should i be doing something with the shouldPerformSegueWithIdentifer method? or should i be leaving it as is above?
Jhoan Arango
14,575 PointsJhoan Arango
14,575 PointsDid you try my code above ? And if so what is it doing ?
Jhoan Arango
14,575 PointsJhoan Arango
14,575 PointsI just modified the code, try it. I don't see why it shouldn't work.
Alexander Certosimo
1,774 PointsAlexander Certosimo
1,774 PointsHi Jhoan,
Was messing around trying to get it to work, but still having no luck. If its set to "" it will give me my invalid entry alert no matter what, if i set it to nil, it will segue to my next view no matter what. I am going to post my code in case i am doing something wrong.