How to add TextField to UIAlertController in Swift

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

    For Swift 4.0, You can use this sample of code succesfully tested in my project:

    @IBAction func withdrawTapped(_ sender: UIButton) {
    
      let alertController = UIAlertController(title: "Token withdraw", message: "", preferredStyle: .alert)
    
      let withdrawAction = UIAlertAction(title: "Withdraw", style: .default) { (aciton) in
    
        let text = alertController.textFields!.first!.text!
    
        if !text.isEmpty {
          self.presentAlert(
            title: "Succesful", 
            message: "You made request for withdraw \(textField.text) tokens")
        }
      }
    
      let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
      }
    
      alertController.addTextField { (textField) in
        textField.placeholder = "999"
        textField.keyboardType = .decimalPad
      }
    
      alertController.addAction(withdrawAction)
      alertController.addAction(cancelAction)
    
      self.present(alertController, animated: true, completion: nil)
    }
    

提交回复
热议问题