How to add a TextField to UIAlertView in Swift

前端 未结 10 1059
耶瑟儿~
耶瑟儿~ 2020-12-23 13:41

I have this code, but I dont know how to show a textfield inside the UIAlertView.

var altMessage = UIAlertController(title: \"Warning\", message: \"This is A         


        
10条回答
  •  自闭症患者
    2020-12-23 14:19

    Updated for swift 3 :

    used below simple code:

        func showAlertWithTwoTextFields() {
    
            let alertController = UIAlertController(title: "Add Event", message: "Enter event and it's description", preferredStyle: .alert)
    
            let saveAction = UIAlertAction(title: "Save", style: .default, handler: {
                alert -> Void in
    
                let eventNameTextField = alertController.textFields![0] as UITextField
                let descriptionTextField = alertController.textFields![1] as UITextField
    
                print("firstName \(String(describing: eventNameTextField.text)), secondName \(String(describing: descriptionTextField.text))")
    
                if eventNameTextField.text != "" || descriptionTextField.text != ""{
    
                }else{
                   // self.showAlertMessageToUser(title: "Alert", messageToUser: "Fields should not be empty, Please enter given info...")
    // Show Alert Message to User As per you want
                }
    
            })
    
            let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: {
                (action : UIAlertAction!) -> Void in
    
            })
    
            alertController.addTextField { (textField : UITextField!) -> Void in
                textField.placeholder = "Enter event Name"
            }
            alertController.addTextField { (textField : UITextField!) -> Void in
                textField.placeholder = "Enter event description in short"
            }
    
            alertController.addAction(saveAction)
            alertController.addAction(cancelAction)
    
            self.present(alertController, animated: true, completion: nil)
        }
    

    // Enjoy Coding...!

提交回复
热议问题