How to add TextField to UIAlertController in Swift

前端 未结 13 708
余生分开走
余生分开走 2020-12-07 20:03

I am trying to show a UIAlertController with a UITextView. When I add the line:

    //Add text field
    alertController.addTextFie         


        
相关标签:
13条回答
  • 2020-12-07 20:31

    Great answer a slight modification to show how the textfield can be used:

    func addRow (row: Int, bodin: String, flag: Int) {
        let alertController = UIAlertController(title: bodin, message: "", preferredStyle: .alert)
        alertController.addAction(UIAlertAction(title: "Save", style: .default, handler: {
            alert -> Void in
            _ = alertController.textFields![0] as UITextField
    
        }))
        alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
    
        alertController.addTextField(configurationHandler: {(textField : UITextField!) -> Void in
            switch flag {
            case 0:
                textField.keyboardType = UIKeyboardType.phonePad
                textField.placeholder = "Enter Number"
            case 1:
                textField.keyboardType = UIKeyboardType.emailAddress
                textField.placeholder = "Enter Email"
            default:
                break
            }
    
        })
    
    0 讨论(0)
提交回复
热议问题