我正在使用Swift创建一个注册对话框,其中包括3个文本字段和1个开关。我已经成功地将三个文本字段添加到了警报中。以下代码显示了相同的内容。
let alertController = UIAlertController(title: "Register", message: "", preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
// ...
exit(0)
}
alertController.addAction(cancelAction)
let OKAction = UIAlertAction(title: "Sign UP", style: .Default) { (action) in
// ...
let name0 = alertController.textFields![0] as UITextField
print("Text field: \(name0.text)")
let email1 = alertController.textFields![1] as UITextField
print("Text field: \(email1.text)")
let company2 = alertController.textFields![2] as UITextField
print("Text field: \(company2.text)")
}
alertController.addAction(OKAction)
alertController.addTextFieldWithConfigurationHandler { (textField) in
textField.placeholder = "Name"
textField.keyboardType = .EmailAddress
}
alertController.addTextFieldWithConfigurationHandler { (textField) in
textField.placeholder = "Email"
textField.secureTextEntry = false
}
alertController.addTextFieldWithConfigurationHandler { (textField) in
textField.placeholder = "Company"
textField.secureTextEntry = false
}
self.presentViewController(alertController, animated: true) {
// ...
}
现在,我需要在Alert View中以编程方式添加一个开关。我们正在使用Swift2进行此操作。这是否可行?我对Swift还很陌生。
func createSwitch () -> UISwitch{ let switchControl = UISwitch(frame: CGRect(x:10, y:10, width:0, height:0)); switchControl.isOn = true switchControl.setOn(true, animated: false); switchControl.addTarget(self, action: #selector(switchValueDidChange), for: .valueChanged); return switchControl } @objc func switchValueDidChange(sender: UISwitch!){ print("开关状态 : \(sender.isOn))") }
- user2153553