从UITextField获取信息

时间:2014-09-16 02:56:00

标签: swift uitextfield

我有一个UITextField,我要求用户使用数字键盘输入一些数据。

  • 数据是字符串还是整数?
  • 如何将输入的数字乘以10,然后将其输出到标签?

3 个答案:

答案 0 :(得分:27)

只需使用text的{​​{1}}属性即可获取值(即字符串)。

然后,使用UITextField方法(返回一个可选项)将其转换为Integer,以便您可以执行数学运算。

toInt()

答案 1 :(得分:1)

Xcode 9 with Swift

//Get reference of UITextView
@IBOutlet weak var inputMealName: UITextField! 

//Get value of input from UITextView in variable
let name: String = inputMealName.text!

//Use input value eg. show alert
  let alert = UIAlertController(title: "Alert", message: name, preferredStyle: UIAlertControllerStyle.alert)

    alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.default, handler: nil))
        self.present(alert, animated: true, completion: nil)

答案 2 :(得分:-2)

您可以通过UITextField属性获取字符串。

  • 数据出现一个字符串。您可以使用字符串方法(intValue,doubleValue ....等)将字符串值转换为数字。

  • 获取号码后,您可以通过其文本属性设置标签输出。