How to add TextField to UIAlertController in Swift

前端 未结 13 760
余生分开走
余生分开走 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:15

    How to add textField to AlertView? Let's keep it short and simple.

    This works for Swift 3.0 and above.

    var nameField: UITextField?
    let alertController = UIAlertController(title: "Add Number", message: nil, preferredStyle: .alert)
    // Add textfield to alert view
    alertController.addTextField { (textField) in
        nameField = textField
    }
    

    First, you instantiate an object of UIAlertController and then you add a text field to it by accessing addTextField member of UIAlertController class.

提交回复
热议问题