How to add text input in alertview of ios 8?

前端 未结 9 2386
眼角桃花
眼角桃花 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:20

    This one is work for me:

    let passwordString = lableGetPassword.text
        var textField: UITextField?
        
        // create alertController
        let alertController = UIAlertController(title: "Password", message: "Save the password. Give a tag name.", preferredStyle: .alert)
        alertController.addTextField { (pTextField) in
            pTextField.placeholder = "Tag Name"
            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: "Save", style: .default, handler: { [self] (pAction) in
            // when user taps OK, you get your value here
            let name = textField?.text
            save(name: name!, password: passwordString!)
            alertController.dismiss(animated: true, completion: nil)
        }))
        
        // show alert controller
        self.present(alertController, animated: true, completion: nil)
    

提交回复
热议问题