Swift2 向UIAlert Swift 2添加第二个文本字段

Swift2 向UIAlert Swift 2添加第二个文本字段,swift2,Swift2,我在将第二个文本字段添加到完成块时遇到问题。 有人能告诉我如何添加一个文本字段吗。 我正在尝试添加self.newFirstNameInput=//这是我不理解的部分吗 func insertNewObject(sender: AnyObject) { let newNameAlert = UIAlertController(title: "Add New User", message: "What's the user's name?", preferredStyle: UIAlertCo

我在将第二个文本字段添加到完成块时遇到问题。 有人能告诉我如何添加一个文本字段吗。 我正在尝试添加self.newFirstNameInput=//这是我不理解的部分吗

func insertNewObject(sender: AnyObject) {
  let newNameAlert = UIAlertController(title: "Add New User", message: "What's the user's name?", preferredStyle: UIAlertControllerStyle.Alert)
  newNameAlert.addTextFieldWithConfigurationHandler { (alertTextField) -> Void in
  self.newLastNameInput = alertTextField
}

newNameAlert.view.setNeedsLayout()

newNameAlert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: nil))
newNameAlert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: addNewUser))
presentViewController(newNameAlert, animated: true, completion: nil)
}
我希望我的回答能帮助你

    var txtField1: UITextField!
    var txtField2: UITextField!

    override func viewDidLoad()
    {
        let alert = UIAlertController(title: "Enter Input", message: "", preferredStyle: UIAlertControllerStyle.Alert)

        alert.addTextFieldWithConfigurationHandler(addTextField1)
        alert.addTextFieldWithConfigurationHandler(addTextField2)

        alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: { (UIAlertAction)in
            print("Cancel")
        }))
        alert.addAction(UIAlertAction(title: "Done", style: UIAlertActionStyle.Default, handler:{ (UIAlertAction)in
            print("Done")
            print("First Name : \(self.txtField1.text!)")
            print("Last Name : \(self.txtField2.text!)")
        }))
        self.presentViewController(alert, animated: true, completion: nil)
    }

    func addTextField1(textField: UITextField!)
    {
        textField.placeholder = "Enter first name"
        txtField1 = textField
    }

    func addTextField2(textField: UITextField!)
    {
        textField.placeholder = "Enter last name"
        txtField2 = textField
    }