I'm not sure if how I worded the question makes much sense so let me summarize.
I have 2 UITexfields and 2 UILabels.
I have it all working properly in terms of when I type into my first textField and hit return it will display my text.
Now I'm not sure how to get the same to apply to my other TextField and Label. I read that if I apply a tag to my textfield in Xcode "1" that I can apply tag "2" to my other textfield.
Here is the code I am using so when I press return it'll display textfield tag "1".
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField.tag == 1 {
nameDisplay?.text = textField.text!
}
return true
}
So to completely round this out, I want to display both separate textfields on each separate label.
Simply add an else to your if.
if textField.tag == 1 {
nameDisplay.text = textField.text
} else {
otherLabel.text = textField.text
}
Please note you do not need the ? or ! in that code.
Another option, if you have outlets for your text fields, is to do the following instead of bothering with tags:
if textField == someTextFieldOutlet {
nameDisplay.text = textField.text
} else {
otherLabel.text = textField.text
}
where someTextFieldOutlet is obviously needs to be the actual name of the appropriate text field outlet you have.
Related
4(Swift4)
I am trying to hide the TextField's text after I end editing the TextField , I tried doing this :
textField.isHidden = true
But it's job is to hide the TextField , what I want to do is that hide TEXT of the textField after Tapping outside the textField..
EDIT : I want to hide it, I don't want asterisk , I want the text to be invisible and when I click on textField again it should appear the written text again
I tried this :
textField.text.isHidden = true
It gives me error if I do this way.
Your time and help will be highly appreciated!
Thank You!
You need to pack the text inside another variable
textContent = textField.text
textField.text = ""
if you want to hide the content like password set
textField.isSecureTextEntry = true
//
var textContent = ""
func textFieldDidEndEditing(_ textField: UITextField) {
textContent = textField.text!
textField.text = ""
}
func textFieldDidBeginEditing(_ textField: UITextField) {
textField.text = textContent
}
Take textfield text into a variable and make textfield empty.
var textFieldText = textfield.text
textField.text = ""
I am trying to sync two different UILabels with the value from two UITextFields, and if there is no data in the text field, then I would like them to keep their label placeholders that I set in the attributes inspector ("Label 1" & "Label 2"):
func textFieldDidEndEditing(_ textField: UITextField) {
labelOne.text = labelOneTextField.text
labelTwo.text = labelTwoTextField.text
}
However, when I finish entering text into the first text field and hit Done, the 2nd label becomes blank. So I tried this:
func textFieldDidEndEditing(_ textField: UITextField) {
if self.labelOneTextField != nil {
labelOne.text = "Label 1"
labelTwo.text = "Label 2"
} else {
labelOne.text = labelOneTextField.text
labelTwo.text = labelTwoTextField.text
}
}
However, this only sets a rule for if the first text field is blank, and it seems very messy. What is a better way to format this, especially considering that there will be many more text fields/labels in this form. Thank you.
Just get the value and check it's not empty:
if let labelOneText = labelOneTextField.text, !labelOneText.characters.isEmpty {
labelOne.text = labelOneText
}
if let labelTwoText = labelTwoTextField.text, !labelTwoText.characters.isEmpty {
labelTwo.text = labelTwoText
}
Select over the textfields:
switch textField {
case labelOneTextField:
labelOne.text = labelOneTextField.text
case labelTwoTextField:
labelTwo.text = labelTwoTextField.text
....
default:
break
}
Requirement
I want to block a text field (UITextField) while another text is being entered in another text field.
Example
I have two text fields on a form. When typing in the first text field, the second text field is disabled (i.e. you can not even press it by touching), only when you finish editing the first text field, you can edit the second one.
You can add your textfield to an an array and use the Equatable protocol to compare the textfield in the DidBeginEditing func and "disable" other fields. But when you are done editing you need to turn all your textfields back on. Also make sure your class has UITextFieldDelegate in the declaration.
var textfields: [UITextField] = [textfield1,textfield2,textfieldn]
func textFieldDidBeginEditing(textField: UITextField) {
for field in textfields {
if textField != field {
field.enabled = false
}
}
}
func textFieldDidEndEditing(textField: UITextField) {
for field in textfields {
field.enabled = true
}
}
I've got two textviews on my view and I want them both to be editable.
But each one pertains to a different record in my database.
How would I be able to detect which textView is being edited?
Here is my code so far
func textViewDidChange(textView: UITextView) { //Handle the text changes here
if(textView.textAlignment == .Center){
PFUser.currentUser()!["bio"] = textView.text
PFUser.currentUser()!.saveInBackground()
}
else{
PFUser.currentUser()!["displayName"] = textView.text
PFUser.currentUser()!.saveInBackground()
}
}
What I'm currently doing is detecting if the view is right aligned or center aligned to be able to tell them apart.
This works but it's not ideal as I'd like to have both of them center aligned. But I'm unaware which field in the textView object would contain an ID or some method of identification as to which textView the function was called for.
As long as you have properties that refer to the two text views you can simply see which one was passed to your delegate and act accordingly:
func textViewDidChange(textView: UITextView) { //Handle the text changes here
guard let currentUser = PFUser.currentUser() else {
return
}
if (textView == self.bioTextView){
currentUser["bio"] = textView.text
currentUser.saveInBackground()
} else {
currentUser["displayName"] = textView.text
currentUser.saveInBackground()
}
}
I have an UITextField which user sets its luggage weight as numbers in TextField. I want to set that textfields value with weight mark (for ex 10 KG which comes from user settings) so whatever user types, there will be KG mark at the end of its textfield. Is there any way for it?
do like
initially clear the value when begin start
func textFieldShouldBeginEditing(textField: UITextField) {
textField.text = ""
}
when editing is over append the kg
func textFieldDidEndEditing(textField: UITextField) {
yourTextfieldName.text = "\(textField.text!) KG"
}
Choice-2
func textFieldDidEndEditing(textField: UITextField)
{
if !textField.text!.rangeOfString("KG").location != NSNotFound {
self.textField.text = textField.text!.stringByAppendingString("KG")
}
}
You could place a "Label" next to the UITextField.
And then just change the text of the Label to whatever the user selects.
OR (but i dont know if that works), try to get the text form the textfield, add the unit (as a string) to the string from the textfield.