AlertView with textbox, how to gather the data from entered textbox - swift

Would like to gather the data from a AlertView out from the textbox. What I've found in the www is this:
#IBAction func showAlertTapped(sender: AnyObject) {
//Create the AlertController
let actionSheetController: UIAlertController = UIAlertController(title: "Add User", message: "Enter username", preferredStyle: .Alert)
//Create and add the Cancel action
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
//Do some stuff
}
actionSheetController.addAction(cancelAction)
//Create and an option action
let saveAction: UIAlertAction = UIAlertAction(title: "Save", style: .Default) { action -> Void in
//Do some other stuff
}
actionSheetController.addAction(saveAction)
//Add a text field
actionSheetController.addTextFieldWithConfigurationHandler { textField -> Void in
//TextField configuration
textField.textColor = UIColor.blueColor()
}
//Present the AlertController
self.presentViewController(actionSheetController, animated: true, completion: nil)
}
Im not aware of the functionality of the AlertController, so I don't know where to bring the entred text to a e.g. simple variable?

Looks like UIAlertController has a textFields property and since you only have one text field you should be able to access it in the following way
let textField = actionSheetController.textFields?.first as UITextField

You can access the textfields string by assigning it to a local variable in the configuration handler.
//Create a local variable
var alertTextField:UITextField?
//Create the AlertController
let actionSheetController: UIAlertController = UIAlertController(title: "Add User", message: "Enter username", preferredStyle: .Alert)
//Create and add the Cancel action
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
//Do some stuff
}
actionSheetController.addAction(cancelAction)
//Create and add save action
let saveAction: UIAlertAction = UIAlertAction(title: "Save", style: .Default) { action -> Void in
//Unwrap your local variable and access your textfield
if let textField = alertTextField {
println("\(textField.text)")
}
}
actionSheetController.addAction(saveAction)
//Add a text field
actionSheetController.addTextFieldWithConfigurationHandler { textField -> Void in
//TextField configuration
textField.textColor = UIColor.blueColor()
//Assign your UIAlertController textField to your local variable
alertTextField = textField
}
//Present the AlertController
self.presentViewController(actionSheetController, animated: true, completion: nil)

Swift 3 version:
//Create a local variable
var alertTextField:UITextField?
//Create the AlertController
let actionSheetController: UIAlertController = UIAlertController(title: "Add User", message: "Enter username", preferredStyle: .alert)
//Create and add the Cancel action
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in
//Do some stuff
}
actionSheetController.addAction(cancelAction)
//Create and add save action
let saveAction: UIAlertAction = UIAlertAction(title: "Save", style: .default) { action -> Void in
//Unwrap your local variable and access your textfield
if let textField = alertTextField {
print(textField.text!)
}
}
actionSheetController.addAction(saveAction)
//Add a text field
actionSheetController.addTextField { textField -> Void in
//TextField configuration
textField.textColor = UIColor.blue
//Assign your UIAlertController textField to your local variable
alertTextField = textField
}
//Present the AlertController
self.present(actionSheetController, animated: true, completion: nil)

Related

How to check and enable Action in a UIAlertController only if the textfield is not empty?

I'm trying to figure out how to keep the Action Button disabled until the user enters some text in, at which point the button would be enabled again. I've been searching around, and some people are suggesting to use Observers? Would that be the best way to go?
Cheers!
#objc func addExercise() {
var textField = UITextField()
let alert = UIAlertController(title: "New Exercise", message: "Please name your Exercise...", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .default) { (UIAlertAction) in
alert.dismiss(animated: true, completion: nil)
}
let addAction = UIAlertAction(title: "Add Exercise", style: .default) { (UIAlertAction) in
//Add Exercise to database.
//Append exercise to selected workout object.
let exercise = Exercises()
exercise.exerciseName = textField.text!
try! self.realm.write {
self.selectedWorkout?.exercise.append(exercise)
self.loadExercises()
}
}
alert.addTextField { (alertTextField1) in
alertTextField1.delegate = self
alertTextField1.placeholder = "Bench Press"
alertTextField1.text = textField.text
textField = alertTextField1
}
alert.addAction(addAction)
alert.addAction(cancelAction)
present(alert, animated: true, completion: nil)
}
Using notification is a way but long work instead you can use simple delegate method or action method of textfield which is much easier as follow:
weak var buttonActionToEnable: UIAlertAction?
alert.addTextField { (alertTextField1) in
alertTextField1.delegate = self
alertTextField1.placeholder = "Bench Press"
alertTextField1.text = textField.text
alertTextField1.addTarget(self, action: #selector(self.textFieldChanged), for: .editingChanged)
}
self.buttonActionToEnable = addAction
addAction.isEnabled = false
#objc func textFieldChanged(_ sender: Any) {
let textfield = sender as UITextField
self.buttonActionToEnable?.isEnabled = textfield.text.count > 0
}

I need to create like a pop up menu where I can change data in a collection view when the collection view cell is pressed.

Basically, the question says it all. I thought about an alert but i need to actually have a text box inside and be able to save the text to a label. Any ideas on what I should use in this? I don't want to create a new view controller into a navigation. I just simply want a pop up view to open and close as needed.
All suggestions are greatly appreciated. Thanks!
You can add a textfield in your alertController.
Please refer to this answer https://stackoverflow.com/a/33000328/1754559
let alertController = UIAlertController(title: "Your alert", message: "", preferredStyle: .alert)
let saveAction = UIAlertAction(title: "Save", style: .default, handler: {
alert -> Void in
guard let textField = alertController.textFields?[0] as UITextField else { return }
yourLabel.text = textField.text
})
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: {
(action : UIAlertAction!) -> Void in })
alertController.addTextField { (textField : UITextField!) -> Void in
textField.placeholder = "Whatever"
//Other textField configurations
}
alertController.addAction(saveAction)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)

How to show second action sheet on clicking a button in first action sheet, Swift

I am able to show the editPhotoActionSheet by calling showActionSheetToEditPhoto().
When I click delete button in editPhotoActionSheet I would like to show deletePhotoActionSheet, but I am not able to do so with the below code.
Please advice how I could achieve this.
Also see both action sheets below.
func showActionSheetToEditPhoto() -> UIAlertController {
let alertController : UIAlertController = UIAlertController()
let takePhoto : UIAlertAction = UIAlertAction(title: "Take Photo", style: .default) { (alert) in
print("User pressed Take Photo")
self.takePhoto()
}
let choosePhoto : UIAlertAction = UIAlertAction(title: "Choose Photo", style: .default) { (alert) in
print("User pressed Choose Photo")
self.choosePhoto()
}
let editPhoto : UIAlertAction = UIAlertAction(title: "Edit Photo", style: .default) { (alert) in
print("User pressed Edit Photo")
self.editExisitingProfilePhoto()
}
let deletePhoto : UIAlertAction = UIAlertAction(title: "Delete Photo", style: .default) { (alert) in
print("User pressed Delete Photo")
self.showDeletePhotoActionSheet()
}
let cancel : UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { (alert) in
print("User pressed Cancel")
}
alertController.addAction(takePhoto)
alertController.addAction(choosePhoto)
alertController.addAction(editPhoto)
alertController.addAction(deletePhoto)
alertController.addAction(cancel)
alertController.popoverPresentationController?.sourceView = view
alertController.popoverPresentationController?.sourceRect = view.frame
return alertController
}
func showDeletePhotoActionSheet() -> UIAlertController {
print("showOptionsToDeletePhoto function called")
self.dismiss(animated: true, completion: nil) // Dismissing previous alert
let alertController : UIAlertController = UIAlertController()
let deletePhoto : UIAlertAction = UIAlertAction(title: "Delete Photo", style: .default) { (alert) in
print("User pressed delete Photo")
var existingPhoto = self.profilePhoto.image
if existingPhoto != nil{
self.profilePhoto.image = nil
}
}
let cancel : UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { (alert) in
print("User pressed Cancel")
}
alertController.addAction(deletePhoto)
alertController.addAction(cancel)
alertController.popoverPresentationController?.sourceView = view
alertController.popoverPresentationController?.sourceRect = view.frame
return alertController
}
EditPhotoActionSheet
DeletePhotoActionSheet
Your method showDeletePhotoActionSheet return UIAlertController instance but you have not used that object to present the action sheet. So present the action alert in your deletePhoto action.
let deletePhoto : UIAlertAction = UIAlertAction(title: "Delete Photo", style: .default) { (alert) in
print("User pressed Delete Photo")
let alert = self.showDeletePhotoActionSheet()
//present delete the action sheet
self.present(alert, animated: true) //Or self.present(self.showDeletePhotoActionSheet(), animated: true)
}

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?>)

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

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()
}