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.
Related
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"
In my app I successfully linked a picker to a text field but then I had some really long pickers. So I made a custom view that is a full screen table view. When I set a textfields inputView to my custom UIView it does not show up. I there anything special you have to do to make it work like a UIPickerView?
SelectView is a UIView component and inputView takes a UIView. I can not find much about this problem since I am new to iOS development and I am probably not searching for the correct thing.
SelectView consist of a label right now just so I first get the view to show up when someone clicks on a text field. Normally when you click on a textfield you can make a PickerView come up which its parent is a UIView. I want my SelectView to come up instead of the picker.
import UIKit
class SelectView: UIView{
#IBOutlet weak var tableView: UITableView!
}
let test = SelectView()
test.tableView.delegate = self
test.tableView.dataSource = self
self.typeTextField.inputView = test
Any direction would be wonderfull
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.
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
I have two view controller that for adding my "Routines". One is to display a list of all routines and one to edit/create routines.
In the first view controller I have two way to enter to the second controller
by selecting an already created routine
by pressing the "+" button on the navigation bar
If the press option is pressed the data should be recalled from CoreData entity and the fields in the second view controller shall be populated.
But I get the error:
fatal error: unexpectedly found nil while unwrapping an Optional value
Would anyone know how to insert content to a custom status cell named "NameCell" which contains a UITextField?
Thanks in advance,
Ace
If you are using static cells in your table view in the storyboard, but want to populate data in one of them, one does not implement any of the UITableViewDataSource methods, but rather just create IBOutlet from the UILabel in question, and then you can populate it as you see fit.
class ViewController: UITableViewController {
#IBOutlet weak var foo: UILabel!
var bar: String! // this is populated by `prepareForSegue` of presenting view controller
override func viewDidLoad() {
super.viewDidLoad()
foo.text = bar
}
}
As the Table View Programming Guide for iOS says:
Note: If a table view in a storyboard is static, the custom subclass of UITableViewController that contains the table view should not implement the data source protocol. Instead, the table view controller should use its viewDidLoad method to populate the table view’s data. For more information, see Populating a Static Table View With Data.