textfield to a label atumatically? - swift

Is there any way in which I can send information from a textfield to a label automatically while writing for example if I have 2 texfield and 1 label
one by name and the other by last name
that when writing a single letter or a name this automatically appears in the label
some way to do this with de button
-> #IBAction func bt(_ sender: Any) { text =texfield!.text text = txfield!.text text.text = "\(textt!) \(texxt!)"

You can add this text change listener in your viewDidLoad method of View Controller.
yourTextField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), for: .editingChanged)
And add this function to your View Controller
#objc func textFieldDidChange(_ textField: UITextField) {
yourLabel.text = textField.text
}

Related

Clearing and restoring placeholder in UITextField

I have an addTarget set up on UITextField that clears the placeholder text when editingDidBegin. But I don't know how to restore the placeholder text if the user clicks on another UITextField, or the field is left empty.
This is located in viewDidLoad()
firstInitialTextField.addTarget(self, action: #selector(clearPlaceholderInTextField), for: UIControl.Event.editingDidBegin)
This function is located in the main body of the program.
#objc func clearPlaceholderInTextField() {
firstInitialTextField.placeholder = ""
}
I have several fields I would like to apply this same functionality to.
Create outlet collection for all the textfields like
#IBOutlet var textFs:[UITextField]!
with different tag for each , And array of placeHolders that you set in viewDidLoad
var placeholders = [String]()
Then set the vc as the delegate for all of them in viewDidLoad
textFs.forEach { $0.delegate = self }
Implement
func textFieldDidBeginEditing(_ textField: UITextField) {
textField.placeholder = ""
}
func textFieldDidEndEditing(_ textField: UITextField) {
textField.placeholder = placeholders[textField.tag]
}

Auto update UITextfield with data from separate UITextfield

I have three UITextfields (customerRequestTextField , fundsAvailableTextField and amountOwedMarketTextField) on the same ViewController.
I would like to set up the amountOwedTextField so that it automatically updates its field by subtracting the data entered into the other two textfields (customerRequestTextField - fundsAvailableTextField).
Should I use NSNotifications? Or a selector? Or something else?
I have tried setting up a selector, but my version does not work.
This is declared globally.
var amountOwedToMarket
This code is located in the viewDidLoad().
amountOwedMarketTextField.text = String(amountOwedToMarket)
amountOwedMarketTextField.addTarget(self, action: #selector(self.textFieldDidChange(sender:)), for: .editingChanged)
}
This is declared outside of the viewDidLoad()
#objc func textFieldDidChange(sender: UITextField) -> Int {
return Int(amountCustomerRequestTextField.text!) ?? 21
}
You need to add the target to textfields that you want to listen to their changes
// in viewDidLoad
amountCustomerRequestTextField.addTarget(self, action: #selector(self.textFieldDidChange), for: .editingChanged)
fundsAvailableTextField.addTarget(self, action: #selector(self.textFieldDidChange), for: .editingChanged)
#objc func textFieldDidChange(_ sender: UITextField) {
if let first = Int(amountCustomerRequestTextField.text!) , let second = Int(fundsAvailableTextField.text!) {
amountOwedMarketTextField.text = "\(first - second)"
}
}

programmatically create .touchUpInside event for UITextField

Added a pickerView using a UITextField.
I am trying to trigger the pickerView programmatically.
func myTypeDetector (textfield: UITextField) {
typeLabel.delegate = self
self.setType(textField: typeLabel)
}
override func viewDidLoad() {
super.viewDidLoad()
typeLabel.addTarget(self, action: #selector(NewSpeciesVC.myTypeDetector(textfield:)), for: UIControlEvents.touchUpInside)
}
I have found how to do this via UIButton
self.typeLabel.sendActions(for: .touchUpInside)
However, this is not working for the UITextField.
I would like to programmatically create the event with the TextField, or create the PickerView event using a UIButton.
Any suggestions?
I needed to do the same thing and figured it out:
self.typelabel.becomeFirstResponder()

Dismiss keyboard for textfield in uitableviewcell in tableviewcontroller - Swift

I have a textfield and textview in my custom tableviewcell.
I have 4 different prototype cell with 4 different class created. there is a textfield in 1 prototype cell and a textview in the other.
I am not sure how I can do it and I dont understand the obj-c answers out there.
I've tried
override func viewDidLoad() {
super.viewDidLoad()
let tap: UIGestureRecognizer = UIGestureRecognizer(target: self, action: #selector(UIInputViewController.dismissKeyboard))
view.addGestureRecognizer(tap) }
and
func dismissKeyboard() {
self.view.endEditing(true)
}
I wanted to try
UITextFieldDelegate and touchesbegan and textfieldshouldreturn method, but there are no textfields to call in my tableviewcontroller.
Go to attributes inspector in storyboard and click tableView and set keyboard to dismiss interactively.
Or
Set textField delegate and implement
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
}
func textFieldShouldReturn(textField: UITextField) -> Bool // called when 'return' key pressed. return false to ignore.
{
textField.resignFirstResponder()
return true
}
I had same problem, you can solve this with IQKeyboardManager, you don't need to complicate things with the custom cell classes, just install it on your project, when you're all set with installation, in the appDelegate inside the didFinishLaunchingWithOptions method, add this line of code:
IQKeyboardManager.shared.shouldResignOnTouchOutside = true
Get IQKeyboardManager from here.

How to add text to an active UITextField

I have a couple of UITextField's and one UIButton. If I tap on the UIButton, I want to place some static text in the active UITextField.
So something like:
#IBAction func buttonEen(sender: UIButton) {
'active textField'.text = "static text"
}
But how do I determine which UITextField is the active UITextField and how do I reach it?
To write text in last active UITextField you have to make your UIViewController a delegate of UITextField
ex:
class ViewController: UIViewController, UITextFieldDelegate
declare a activeField variable
ex:
var activeField: UITextField?
then implements textFieldDidBeginEditing
ex:
func textFieldDidBeginEditing(textField: UITextField)
{
activeField = textField
}
So your button function will be something like
#IBAction func buttonEen(sender: UIButton) {
if activeField
{
activeField.text = "static text"
}
}
First you need to create your textfield delegate (probably in 'ViewDidLoad()') :
activeTxtField.delegate = self
Then you need to implement the textField delegate function:
extension 'YourVC': UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == activeTxtField {
// Do whatever you want
// e.g set textField text to "static text"
}
}
}