1

I would like to a let user create a folder in my app. More specifically, when the user presses "add folder" button, I would like to have the following text field to pop up

enter image description here

How can I implement it?

Sulthan
  • 128,090
  • 22
  • 218
  • 270
iwbtrs
  • 358
  • 4
  • 13

2 Answers2

7

It's going to look something like this:

// create the actual alert controller view that will be the pop-up
let alertController = UIAlertController(title: "New Folder", message: "name this folder", preferredStyle: .alert)

alertController.addTextField { (textField) in
    // configure the properties of the text field
    textField.placeholder = "Name"
}


// add the buttons/actions to the view controller
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
let saveAction = UIAlertAction(title: "Save", style: .default) { _ in

    // this code runs when the user hits the "save" button

    let inputName = alertController.textFields![0].text

    print(inputName)

}

alertController.addAction(cancelAction)
alertController.addAction(saveAction)

present(alertController, animated: true, completion: nil)

Let me know if this helps.

Sylvan M.
  • 160
  • 7
  • Thank you! And one more thing: How to disable "Save" button if the text field is empty? – iwbtrs Mar 09 '20 at 22:10
  • I believe this is a little more complicated, but one way I know of is making your view controller class the delegate of the text field. Add a function that is called whenever the text field is edited, and from there you can set the "isEnabled" property of the button as needed. If you need any help doing this, let me know. – Sylvan M. Mar 09 '20 at 22:16
  • 2
    @Nelver validation answered here https://stackoverflow.com/questions/30596851/how-do-i-validate-textfields-in-an-uialertcontroller/39856501#39856501 – imike Mar 10 '20 at 17:33
2

For custom PopUp:

By Storyboard

Step 1: Create a UIViewController with this pop up message.

Step 2: Intead of push, Present it from your parent View and set Parent View transition style to cross dissolve.

enter image description here

By Code:

if let nav = self.navigationController
{
    UIView.transition(with:nav.view, duration:0.25, options:.transitionCrossDissolve, animations: {
        _ = nav.popViewController(animated:false)
    }, completion:nil)
}
Abdul Karim Khan
  • 4,256
  • 1
  • 26
  • 30