Retrieving Information from UITextField

I have UITextField, and I ask the user to enter some data using the numeric keypad.

  • Is the data outputting a string or an integer?
  • How do I multiply the number they enter by 10, then output it to the label?
+4
source share
3 answers

Just use the attribute text UITextFieldto get the value (which is a string).

Then use a method toInt()(which returns optional) to convert it to Integer so you can perform math 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)"
}
+19
source

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)
0
source

UITextField.

  • data displays a string. You can use string methods (intValue, doubleValue .... etc.) Convert the string value to a number.

  • when you get the number, you can set the label output through your text attribute.

-2
source

All Articles