从 Swift 中的 iOS 警报中的 TextField 获取输入值

编程入门 行业动态 更新时间:2024-10-05 05:24:59
本文介绍了从 Swift 中的 iOS 警报中的 TextField 获取输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用输入生成警报消息,然后从输入中获取值.我发现了很多很好的教程如何制作输入文本字段.但我无法从警报中获取值.

I'm trying to make an alert message with input, and then get the value from the input. I've found many good tutorials how to make the input text field. but I can't get the value from the alert.

推荐答案

针对 Swift 3 及更高版本更新:

Updated for Swift 3 and above:

//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)

斯威夫特 2.x

Swift 2.x

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

Assuming you want an action alert on 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)

更多推荐

从 Swift 中的 iOS 警报中的 TextField 获取输入值

本文发布于:2023-11-26 23:15:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635602.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   Swift   iOS   TextField

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!