gpt4 book ai didi

ios - 如何从 UIAlertController 文本字段获取输入值?

转载 作者:行者123 更新时间:2023-11-30 10:48:42 26 4
gpt4 key购买 nike

我正在尝试创建一个带有消息和输入的警报 Controller ,然后从输入中获取值。我找到了很多关于如何制作输入文本字段的好教程,但我无法从警报中获取值。

最佳答案

针对 Swift 3 及更高版本进行了更新:

//1. Create the alert controller.
let alert = UIAlertController(title: "Some Title", message: "Enter a text", preferredStyle: .alert)

//2. Add the text field. You can configure it however you need.
alert.addTextField { (textField) in
textField.text = "Some default text"
}

// 3. Grab the value from the text field, and print it when the user clicks OK.
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { [weak alert] (_) in
let textField = alert.textFields![0] // Force unwrapping because we know it exists.
print("Text field: \(textField.text)")
}))

// 4. Present the alert.
self.present(alert, animated: true, completion: nil)
<小时/>

swift 2.x

假设您想要在 iOS 上收到操作提醒:

//1. Create the alert controller.            
var alert = UIAlertController(title: "Some Title", message: "Enter a text", preferredStyle: .Alert)

//2. Add the text field. You can configure it however you need.
alert.addTextFieldWithConfigurationHandler({ (textField) -> Void in
textField.text = "Some default text."
})

//3. Grab the value from the text field, and print it when the user clicks OK.
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { [weak alert] (action) -> Void in
let textField = alert.textFields![0] as UITextField
println("Text field: \(textField.text)")
}))

// 4. Present the alert.
self.presentViewController(alert, animated: true, completion: nil)

关于ios - 如何从 UIAlertController 文本字段获取输入值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55146868/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com