Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting info from UITextField

Tags:

I have a UITextField and I ask the user to input some data using the number pad.

  • Is the data that comes out a string or an integer?
  • How do I multiply the number they input by 10 then output it to a label?
like image 293
dsgfdg Avatar asked Sep 16 '14 02:09

dsgfdg


People also ask

How do you get the text that the user has inputted into the textfield Swift?

swift. Press the ctrl key and click the textfield. Now drag it in view controller.

How can I tell if textfield is edited?

You can make this connection in interface builder. In your storyboard, click the assistant editor at the top of the screen (two circles in the middle). Ctrl + Click on the textfield in interface builder. Drag from EditingChanged to inside your view controller class in the assistant view.

What is a text field on Iphone?

A control that displays an editable text interface. iOS 13.0+ iPadOS 13.0+ macOS 10.15+ Mac Catalyst 13.0+ tvOS 13.0+ watchOS 6.0+

What is UITextField?

An object that displays an editable text area in your interface.


2 Answers

Just use the text attribute of the UITextField to get the value (which is a String).

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)" } 
like image 56
AstroCB Avatar answered Oct 17 '22 20:10

AstroCB


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) 
like image 37
Hitesh Sahu Avatar answered Oct 17 '22 21:10

Hitesh Sahu