Getting the value in the textfield in IOS Simulator - ios

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"

Related

How to enable/disable navigation bar buttons [XCode] (swift)

I was wondering how I would be able to go about changing the status of buttons. I want to make it if one of two text fields has text in them then the button will become useable. I have currently turned off the button from the storyboard. The code I have to check if there is text inside of the text fields is as follows:
Disclaimer:
The code to check if the text field has any text in it works perfectly fine.
#IBAction func textFeildEditingChanged(_ textField: UITextField) {
if FirstName.hasText == true {
self.navigationItem.rightBarButtonItem?.isEnabled = true
print("First name isn't empty")
}
}
The current code that I have in there to set the button to enabled and disable doesn't work however the code to test if the text field has content does work. I just need to figure out how to disable and enable the button of a navigation item.
code that doesn't work is below:
self.navigationItem.rightBarButtonItem?.isEnabled = true
Any help would be greatly appreciated.
Edit: I am using a navigation controller, don't know if that's important or not.
If you are using the storyboard, just connect the outlet then disable it.
#IBOutlet var navigationItemButton: UIBarButtonItem!
then
navigationItemButton.isEnabled = FirstName.hasText

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 to disable UIControlEventEditingChanged

I have a UIViewController with several UITextFields. When tap one text field, it should present the barcode scanning view controller. Once the scanning is completed, my barcode scanning viewcontroller is disappearing (used "dismissViewcontroller") and the scanned value should entered into the text field I tapped. This is working fine. I have set the delegate for each text field like this.
[field addTarget:metrixUIViewControllerIn action:#selector(executeScriptOnTextFieldChange:) forControlEvents:UIControlEventEditingChanged];
The problem is this :
Lets say I have set an alert to display inside this executeScriptOnTextFieldChange method. Once I tapped on the 1st text field, then the barcode scanner comes. Once I scanned barcode scanner closes and set the value for the first text field and fire the alert.Thats ok. But then if scanned by tapping the 2nd textfield and the string will set to that textfield and fire the alert related to 2nd textfield also fire the alert related to first textfield as well. I want to stop happening this. Is there any way to disable the delegate for one textfield? This happens because I am refreshing the view in the viewDidAppear. But I have to do that as well. Please help me.
UIControlEventEditingChanged for a textField can fire at many different events that are not even directly related to that textField, but related inderectly.
For instance, when your ViewController is presenting the barcodeScanner it may trigger a "resignFirstResponder" event on the textField. Also when the 2nd textField is tapped, cause the 2nd becomes first responder and the 1st suffers a "resignFirstResponder".
I suggest trying to use a UITapGestureRecognizer in your textField instead. Example:
Swift 4
#IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
self.textField.tag = 1
self.textField.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(fireTextField(_:))))
}
#objc func fireTextField(_ sender: UIGestureRecognizer){
let view = sender.view
guard view != nil else{
//Do nothing
return
}
let condition = view!.tag == 1
if condition{
//or do whatever other stuff you need
self.textField.becomeFirstResponder()
}else{
//Whatever for other textFields
}
}
This way, you could use the "tag" attribute to determine which textField is firing and so adjust "condition". You could also filter the flow with a switch using the "tag".
Not sure if any of this will really help as I would need more info about the flow you need to accomplish. Hope it does help!

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.

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

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

Resources