迅速2.0:
1)使一个公共变量
var alertController: UIAlertController?
2)设置警报控制器
alertController = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: .Alert)
//old password textfield
alertController!.addTextFieldWithConfigurationHandler({(textField: UITextField!) in
textField.placeholder = "Password"
textField.secureTextEntry = true
textField.addTarget(self, action: "alertTextFieldDidChange:", forControlEvents: .EditingChanged)
})
//new password textfield
alertController!.addTextFieldWithConfigurationHandler({(textField: UITextField) -> Void in
textField.tag = 1002
textField.placeholder = "New Password"
textField.secureTextEntry = true
textField.addTarget(self, action: "alertTextFieldDidChange:", forControlEvents: .EditingChanged)
})
//confirm password textfield
alertController!.addTextFieldWithConfigurationHandler({(textField: UITextField) -> Void in
textField.tag = 1003
textField.placeholder = "Confirm Password"
textField.addTarget(self, action: "alertTextFieldDidChange:", forControlEvents: .EditingChanged)
})
alertController!.addAction(UIAlertAction(title: "Submit", style: UIAlertActionStyle.Default, handler: { (action) -> Void in
//Do after submit is clicked
}))
alertController!.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: { (action) -> Void in
self.alertController?.dismissViewControllerAnimated(true, completion: nil)
}))
self.presentViewController(alertController!, animated: true, completion: nil)
3)以文本字段值
func alertTextFieldDidChange(sender: UITextField) {
alertController = self.presentedViewController as? UIAlertController
let firstTextField: UITextField = (alertController?.textFields![0])!
let secondTextField: UITextField = (alertController?.textFields![1])!
let thirdTextField: UITextField = (alertController?.textFields![2])!
print(firstTextField.text)
print(secondTextField.text)
print(thirdTextField.text)
}
说明:
您定位的是哪个操作系统?您应该看看UIAlertControlle:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/ –
[如何将多个UITextField添加到iOS 7中的UIAlertView?](http:/ /stackoverflow.com/questions/19090547/how-to-add-multiple-uitextfields-to-a-uialertview-in-ios-7) –
你应该做一个自定义警报来添加文本字段,只需制作一个自定义的UIView类并使它看起来像你想要的。然后在你的类中显示它而不是调用UIAlertView。 –