从 UITextField 获取信息 [英] Getting info from UITextField
本文介绍了从 UITextField 获取信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 UITextField
,我要求用户使用数字键盘输入一些数据.
I have a UITextField
and I ask the user to input some data using the number pad.
- 出来的数据是字符串还是整数?
- 如何将他们输入的数字乘以 10,然后将其输出到标签?
推荐答案
只需使用 UITextField
的 text
属性来获取值(它是一个字符串).
Just use the text
attribute of the UITextField
to get the value (which is a String).
然后,使用 toInt()
方法(返回一个可选的)将其转换为整数,以便您可以执行数学运算.
Then, use the toInt()
method (which returns an optional) to convert that to an Integer, so that you can perform mathematical operations.
@IBOutlet weak var field: UITextField!
@IBOutlet weak var label: UILabel!
@IBAction func getVal () {
var text: String = field.text
var multipliedNum: Int = 0
if let num = text.toInt() {
multipliedNum = num * 10
}
label.text = "\(multipliedNum)"
}
这篇关于从 UITextField 获取信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文