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

时间:2016-03-28 17:26:16

标签: swift2

我在向完成块添加第二个文本字段时遇到问题。 有人可以告诉我如何添加一个文本域请。 我想添加一个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)
}

1 个答案:

答案 0 :(得分:0)

    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
    }

我希望我的回答可以帮助你...