App*_*Kid 39 uialertview ios swift
我有这个代码,但我不知道如何在UIAlertView中显示文本字段.
var altMessage = UIAlertController(title: "Warning", message: "This is Alert Message", preferredStyle: UIAlertControllerStyle.Alert)
altMessage.addAction(UIAlertAction(title: "Done", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(altMessage, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
我有这个代码用于textfield,如何在UIAlerView中显示它
var my:UITextField = UITextField(frame: CGRectMake(0, 0, 10, 10))
Run Code Online (Sandbox Code Playgroud)
我也试过这段代码:
var alert = UIAlertView()
alert.title = "Enter Input"
alert.addButtonWithTitle("Done")
alert.alertViewStyle = UIAlertViewStyle.PlainTextInput
alert.addButtonWithTitle("Cancel")
alert.show()
Run Code Online (Sandbox Code Playgroud)
当我指定AlertStyle明文,它显示了默认占位符一个TextField,"登录".我想改变这种状况,我想说明小数垫的键盘.我还想处理用户输入textField的值.有人可以帮我弄这个吗?
Dav*_*rry 35
您可以使用以下方式访问文本字段:
let textField = alert.textFieldAtIndex(0)
Run Code Online (Sandbox Code Playgroud)
然后更改占位符文本:
textField.placeholder = "Foo!"
Run Code Online (Sandbox Code Playgroud)
和键盘类型:
textField.keyboardType = ...
Run Code Online (Sandbox Code Playgroud)
Guy*_*lon 22
试试这个代码(快速):
func configurationTextField(textField: UITextField!)
{
println("configurat hire the TextField")
if let tField = textField {
self.textField = textField! //Save reference to the UITextField
self.textField.text = "Hello world"
}
}
func handleCancel(alertView: UIAlertAction!)
{
println("User click Cancel button")
println(self.textField.text)
}
var alert = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addTextFieldWithConfigurationHandler(configurationTextField)
alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Cancel, handler:handleCancel))
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler:{ (UIAlertAction)in
println("User click Ok button")
println(self.textField.text)
}))
self.presentViewController(alert, animated: true, completion: {
println("completion block")
})
Run Code Online (Sandbox Code Playgroud)
你能在这里看到我的答案吗?
Dar*_*hah 15
在目标C中
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Duplicate file" message:@"A file with the same name already exists." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[[alertView textFieldAtIndex:0] setText:@"Filename"];
[[alertView textFieldAtIndex:0] setPlaceholder:@"Enter Filename"];
[alertView show];
Run Code Online (Sandbox Code Playgroud)
在Swift 2.3中
func doSomething(){
var alert = UIAlertController(title: "Duplicate file", message: "A file with the same name already exists.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addTextFieldWithConfigurationHandler(configurationTextField)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler:{ (UIAlertAction)in
print("User click Ok button")
print(self.textField.text)
}))
self.presentViewController(alert, animated: true, completion: {
print("completion block")
})
}
func configurationTextField(textField: UITextField!){
textField.text = "Filename"
}
Run Code Online (Sandbox Code Playgroud)
在Swift 3中
func doSomething(){
var alert = UIAlertController(title: "Duplicate file", message: "A file with the same name already exists.", preferredStyle: UIAlertControllerStyle.alert)
alert.addTextField(configurationHandler: configurationTextField)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler:{ (UIAlertAction)in
print("User click Ok button")
print(self.textField.text)
}))
self.present(alert, animated: true, completion: {
print("completion block")
})
}
func configurationTextField(textField: UITextField!){
textField.text = "Filename"
}
Run Code Online (Sandbox Code Playgroud)
Sab*_*esh 13
var inputTextField: UITextField?
//Create the AlertController
let actionSheetController: UIAlertController = UIAlertController(title: "Rename", message: "", preferredStyle: .Alert)
//Create and add the Cancel action
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
//Do some stuff
}
actionSheetController.addAction(cancelAction)
//Create and an option action
let nextAction: UIAlertAction = UIAlertAction(title: "OK", style: .Default) { action -> Void in
//Do some other stuff
}
actionSheetController.addAction(nextAction)
//Add a text field
actionSheetController.addTextFieldWithConfigurationHandler { textField -> Void in
// you can use this text field
inputTextField = textField
}
//Present the AlertController
self.presentViewController(actionSheetController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
小智 11
在Swift 3中
let alert = UIAlertController(title: "Alert Ttitle", message: "Alert Message", preferredStyle:
UIAlertControllerStyle.alert)
alert.addTextField(configurationHandler: textFieldHandler)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler:{ (UIAlertAction)in
}))
self.present(alert, animated: true, completion:nil)
Run Code Online (Sandbox Code Playgroud)
func textFieldHandler(textField: UITextField!)
{
if (textField) != nil {
textField.text = "Filename"
}
}
Run Code Online (Sandbox Code Playgroud)
Jim*_*y_m 11
public func alertWithTextField(title: String? = nil, message: String? = nil, placeholder: String? = nil, completion: @escaping ((String) -> Void) = { _ in }) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addTextField() { newTextField in
newTextField.placeholder = placeholder
}
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel) { _ in completion("") })
alert.addAction(UIAlertAction(title: "Ok", style: .default) { action in
if
let textFields = alert.textFields,
let tf = textFields.first,
let result = tf.text
{ completion(result) }
else
{ completion("") }
})
navigationController?.present(alert, animated: true)
}
Run Code Online (Sandbox Code Playgroud)
用法:
alertWithTextField(title: "bork", message: "borkborkbork", placeholder: "bork?") { result in
print(result)
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特4:
var textField: UITextField?
func configurationTextField(textField: UITextField!) {
if (textField) != nil {
self.textField = textField! //Save reference to the UITextField
self.textField?.placeholder = "Some text";
}
}
func openAlertView() {
let alert = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: UIAlertControllerStyle.alert)
alert.addTextField(configurationHandler: configurationTextField)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler:nil))
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler:{ (UIAlertAction) in
print("User click Ok button")
}))
self.present(alert, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
55560 次 |
| 最近记录: |