Reputation:
How can i set the value of a textfield and then edit it? Using firebase, I created a dictionary for a user account that contains a name. For the first textfield, the textfield text is equal to the user's name. When the user begins to edit the name using through that textfield, another variable called "username" should store the new text/value, but it doesn't. The user can edit the textfield itself, but the "username" variable doesn't store the edited version, only the original version (user.name) thats stored in firebase. Here's my code below:
var username: String?
case 0:
firstfewCells.ttField.text = user.name
self.username = firstfewCells.ttField.text
What I want to do is make the textfield editable so that whatever is in the textfield when the save button is pressed, will be stored
Upvotes: 0
Views: 70
Reputation: 301
var name : String?
override func awakeFromNib() {
yourTextField.addTarget(self, action: #selector(self.doSomething(_:)), forControlEvents: UIControlEvents.EditingChanged)
}
func doSomething() {
yourTextField.text
}
If you want to send this info to your ViewController, You need to create a delegate. If you need with that, you can ask. But there are quite a few tutorials explaining how it works.
Upvotes: 0
Reputation: 321
You've set the value of username
to user.name
but in all likelihood that value is a copy and you shouldn't expect changes to the 'username' to result from changes to the textfield.
There are ways for you to listen to changes to the textfield, the simplest being through the uitextfielddelegate
method textfielddidchange
(I'll leave it to you to google).
However, what I recommend you do, if you just want to provide a form-like interface to allow a user to change values that will then be POSTed to a server is to simply grab the values of the text fields when the save or submit button is pressed (let username = nameTextField.text
). This simplifies your code in that you will eliminate unneeded state and all complexity that would come with keeping it in sync.
Upvotes: 1