Xcode How read numbers on a Text View (or label?) - ios

this is my first post.
I'm new in the Xcode Word.
At the moment I'm working to a simple application and I need to read a number from a box and after a lot of calculations write the result in an other text view or label.
I need how to read a numbers or a string from a box after writing (text view, label or other).
Thank you very much for your answers

if you are sure that your textfield will accept only numbers, one of the possible ways could be:
yourTextField.keyboardType = UIKeyboardType.decimalPad
in this way, when the user will tap on the textField, you may use the numeric keypad.
At this point your viewController must implement UItextFieldDelegate
yourTextField.delegate = self
and then use the method, called when textfield loses focus:
func textFieldDidEndEditing(_ textField: UITextField) {
var valueInserted : Float = NSString(string: textField.text!).floatValue
}

Drag a UITextView inside the scene (in the storyboard)
Create an outlet between the UITextView and ViewController.swift
in ViewController.swift you will have something like this:
#IBOutlet weak var myTextView:UITextView!
You must also get a reference for your label
#IBOutlet weak var myLabel:UILabel!
you can now access to the content(input) of your textView by checking the text property:
var input = myTextView.text
This will be a string, that you can try to convert to an Int. Once you have a result to display, set the label text:
myLabel.text = result

Related

Getting the value in the textfield in IOS Simulator

I am new to Swift and x-code. I'm currently developing an app where there is only one textbox and one submit button.
I am writing a test case which is like this- "Whenever i click the submit button, the text which is given by me in the text field should be validated."
How can i get the text from the textbox when i click the submit button? while running the test case
To find out when the button is clicked, you will have to define an Action for your button in some Target... usually your view controller. Search Apple’s documentation for the “Target Action” design pattern and you should find tutorial information.
To get the value of a text field, you can simply get it’s text property. However, to get a reference to the text field, your view controller will need an Outlet that references the text view. In the documentation you can search for “Outkests” or IBOutlet to find tutorials.
Create an IBOutlet for that textField and connect it in the storyboard (See attached file)
#IBOutlet weak private var textField: UITextField!
And use textField.text to get text file value in your IBAction that handle tap event
first create #IBOutlet of your text field and create #IBAction of submit button. and validate it in #IBAction like this :-
#IBOutlet weak var textfieldOutlet: UITextField!
#IBAction func submit(_ sender: UIButton)
{
if textfieldOutlet.text?.isEmpty == False
{
print(textfieldOutlet.text);
}
}
*set breakpoint on print value and you getvalue
In UI tests, you can access the text from an XCUIElement representing a UITextField using the value property.
let app = XCUIApplication()
let textField = app.textFields.element
textField.typeText("test text")
let text = textField.value as? String // should be "test text"

UITextField change in iOS for conversions

I am creating a conversion application. I have multiple text fields. What I intend to do is when I start entering Kg value its equivalent gram and pound value should be displayed in the gram and pound text fields. How can it be done using editingChanged function in iOS?
This question is very broad but I will try to give a short answer of how to track changed of text fields:
class YourViewController: UIViewController {
#IBOutlet weak var textField: UITextField!
#IBAction func textFieldDidChange(_ sender: UITextField) {
self.textField.text = doSomeCalculation(sender.text)
}
}
First, you need to set up a view controller (or a view or whatever suits your interface design) with a UITextField. Then you connect the UITextField with the variable textField and the desired action of the text field with the function textFieldDidChange. You can use the action Editing Changed but I prefer Value Changed (depends on what you want to achieve).
Any time the text field is changed the function textFieldDidChange is called and you can do your calculations and alter the text of textField.

how can i create UITextField based on a user input in swift

How can I create UITextField based on a user input in swift
I tried to do it but it's not possible to drag and drop so many text fields !
For example, I've a UITextField if a user enter 5< for example, then a 5 UITextField's will be created I need it in another view controller and I need to control them separately
how can I do it ?
#IBOutlet weak var numberOfTextFields: UITextField!
#IBAction func the_action_button(sender: AnyObject) {
// ?
}
I guess you can use tableView, and create a cell with a text field in it. And when a user enters 5 in your text field that would mean that table view should have 5 rows.
Or you can create those 5 text fields from code not from interface builder.

Setting prompt to beginning of text in text field

I'm trying to set the the prompt to the beginning of some text in a UITextField based on this accepted answer Getting and Setting Cursor Position of UITextField and UITextView in Swift
However the prompt is getting set at the end, not the beginning. Has something changed in iOS10/Swift 3 to invalidate the code in the answer in that question?
#IBOutlet weak var mdnTextField: UITextField!
mdnTextField.text = "Some text"
mdnTextField.tintColor = UIColor.textFieldPromptLightGray()
mdnTextField.textColor = UIColor.textFieldPromptLightGray()
let promptPosition = mdnTextField.beginningOfDocument
mdnTextField.selectedTextRange = mdnTextField.textRange(from: promptPosition, to: promptPosition)
Move the promptPosition stuff to your text field's delegate's didBeginEditing.

Changing text of Swift UILabel

I am attempting to learn Apple's Swift. I was recently trying to build a GUI app, but I have a question:
How do I interact with GUI elements of my app? For instance, I used interface builder to make a UILabel, and I connected it to my viewcontroller by control-clicking, so that I get the #IBOUTLET thing. Now, how do I, while in my view controller, edit the text of this UILabel? To state it another way, what code can I use to programatically control the text of something on my storyboard? All methods I have found online only appear to work with a button generated in code, not a button generated on a storyboard.
I've seen code like
self.simpleLabel.text = "message"
If this is right, how do I link it with the label in question? In other words, how do I adapt this code to be connected with the IBOutlet (If that's what I do)
If you've successfully linked the control with an IBOutlet on your UIViewController class, then the property is added to it and you would simply replace simpleLabel with whatever you named your IBOutlet connection to be like so:
class MyViewController: UIViewController {
#IBOutlet weak var myLabel: UILabel!
func someFunction() {
self.myLabel.text = "text"
}
}
The outlet you created must've been named by you. The outlet belongs to your view controller. self.simpleLabel means 'fetch the value of my property named 'simpleLabel'.
Since different outlets have different names, using self.simpleLabel here won't work until your outlet is named 'simpleLabel'. Try replacing 'simpleLabel' with the name you gave to the outlet when you created it.
The correct way now would be:
self.yourLabelName.text = "message"
If you have something like this for an IBOutlet:
#IBOutlet var someLabel: UILabel!
then you could set the text property just like in your example:
someLabel.text = "Whatever text"
If you're having problems with this, perhaps you're not assigning the text property in the right place. If it's in a function that doesn't get called, that line won't execute, and the text property won't change. Try overriding the viewDidLoad function, and put the line in there, like this:
override func viewDidLoad() {
super.viewDidLoad()
someLabel.text = "Whatever text"
}
Then, as soon as the view loads, you'll set the text property. If you're not sure if a line of code is executing or not, you can always put a breakpoint there, or add some output. Something like
println("Checkpoint")
inside a block of code you're unsure about could really help you see when and if it runs.
Hope this helps.
You may trying to change a UI component not in the main thread, in that case, do this:
DispatchQueue.main.async {
someLabel.text = "Whatever text"
}

Resources