Change the text of a UITextField on text change of another text field
Solution 1
Create an Array :
var myArray= [String]()
In viewDidLoad do this, Assign action when user did some changes in Text Fields(do this for all textfield/1,2,3,4,5):
textField1.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)
textField2.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)
when user edits something this method will be called:
func textFieldDidChange(textField: UITextField) {
myArray.removeAll()
if !textField1.isEmpty {
myArray.append(textField1.text)
}
if !textField2.isEmpty {
myArray.append(textField2.text)
}
if !textField3.isEmpty {
myArray.append(textField3.text)
}
if !textField4.isEmpty {
myArray.append(textField4.text)
}
if !textField5.isEmpty {
myArray.append(textField5.text)
}
textField6.text = myArray.joinWithSeparator(":")
}
Solution 2
You can implement didEndEditing
delegate and do your logic there.
func textFieldDidEndEditing(textField: UITextField) {
var text1:String?
var text2:String?
var text3:String?
var text4:String?
var text5:String?
if ((textField1.text) != nil) {
text1 = textField1.text
} else {
text1 = ""
}
if ((textField2.text) != nil) {
text2 = textField2.text
} else {
text2 = ""
}
if ((textField3.text) != nil) {
text3 = textField3.text
} else {
text3 = ""
}
if ((textField4.text) != nil) {
text4 = textField4.text
} else {
text4 = ""
}
if ((textField5.text) != nil) {
text5 = textField5.text
} else {
text5 = ""
}
textField6.text = String(format: "%@:%@:%@:%@:%@", text1, a2, a3, a4, a5)
}
Add textfield delegate and implement it to textfield1 - textfield5 to
Solution 3
You can reach to specific textfields using by their names. For example ;
func textFieldDidEndEditing(textField: UITextField) {
sixthTextField.text = "\(firstTextField.text):\(secondTextField.text)\(thirdTextField.text):\(fourthTextField.text)\(fifthTextField.text)"
}
This method will execute the code when textField's end editing. If you wanna check it for specific textField ;
func textFieldDidEndEditing(textField: UITextField) {
if (textField == secondTextfield) {
sixthTextField.text = "\(firstTextField.text):\(secondTextField.text)\(thirdTextField.text):\(fourthTextField.text)\(fifthTextField.text)"
}
}
This will execute the code only if secondTextField's end editing.
Don't forget to add UITextFieldDelegate to your class and don't forget to set textField's delegates in viewDidLoad.
class MyClass: UIViewController, UITextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
firstTextField.delegate = self
secondTextField.delegate = self
thirdTextField.delegate = self
fourthTextField.delegate = self
fifthTextField.delegate = self
sixthTextField.delegate = self
}
}
Or you can create IBAction for each textField for valueChanged event. Same as UIButton IBAction only diffrence is the event. You should choose valueChanged as event.
@IBAction func firstTextFieldValueChanged(sender: UITextField) {
sixthTextField.text = "\(firstTextField.text):\(secondTextField.text):\(thirdTextField.text):\(fourthTextField.text):\(fifthTextField.text)"
}
@IBAction func secondTextFieldValueChanged(sender: UITextField) {
sixthTextField.text = "\(firstTextField.text):\(secondTextField.text):\(thirdTextField.text):\(fourthTextField.text):\(fifthTextField.text)"
}
@IBAction func thirdTextFieldValueChanged(sender: UITextField) {
sixthTextField.text = "\(firstTextField.text):\(secondTextField.text):\(thirdTextField.text):\(fourthTextField.text):\(fifthTextField.text)"
}
@IBAction func fourthTextFieldValueChanged(sender: UITextField) {
sixthTextField.text = "\(firstTextField.text):\(secondTextField.text):\(thirdTextField.text):\(fourthTextField.text):\(fifthTextField.text)"
}
@IBAction func fifthTextFieldValueChanged(sender: UITextField) {
sixthTextField.text = "\(firstTextField.text):\(secondTextField.text):\(thirdTextField.text):\(fourthTextField.text):\(fifthTextField.text)"
}
zack_falcon
I'm currently an IT Student. I love computer games, good food, skating, football, and R/C Trucks.
Updated on June 05, 2022Comments
-
zack_falcon almost 2 years
I've got six text fields. One of those fields cannot be edited by the user. Instead, it takes in the input from the other five, condenses it into one string (with colons in between each input, with a certain arrangement).
Anytime one modifies any of the enabled textfields, the sixth text field's content changes, and the order of the input does not change. So if I typed in
1
,2
,3
,4
,5
in the first five textfields, the sixth would read1:2:3:4:5
. If I change the second textfield's text from2
totwo
, the sixth textfield's would also change to1:two:3:4:5
.I've an idea of how to do this, create five global variables, save the values of the five textfields, and then have the sixth read from a concat of those. I can't figure out how to trigger on the text change.
I've tried using this:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { }
along with other delegate methods, but I'm not sure that's the way to go.
-
zack_falcon over 8 yearsThanks. Of note is that
":".join(myArray)
doesn't work on Swift 2. Gotta usemyArray.joinWithSeparator(":")
instead.