How to add text input in alertview of ios 8?

前端 未结 9 2409
眼角桃花
眼角桃花 2020-12-07 16:46

I want to add text input in alert-view of ios 8. I know it was done using UIAlertController but not have any idea. How to do it ?

9条回答
  •  北荒
    北荒 (楼主)
    2020-12-07 17:09

    Example of implementation with Swift 3:

    var textField: UITextField?
    
    // create alertController
    let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
      alertController.addTextField { (pTextField) in
      pTextField.placeholder = "usefull placeholdr"
      pTextField.clearButtonMode = .whileEditing
      pTextField.borderStyle = .none
      textField = pTextField
    }
    
    // create cancel button
    alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (pAction) in
      alertController.dismiss(animated: true, completion: nil)
    }))
    
    // create Ok button
    alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { (pAction) in
      // when user taps OK, you get your value here
      let inputValue = textField?.text
      alertController.dismiss(animated: true, completion: nil)
    }))
    
    // show alert controller
    self.present(alertController, animated: true, completion: nil)
    

提交回复
热议问题