Getting Info from Uitextfield

How to get user input from UItextfield and add it to an array?

As far as I could suggest (without any code insight given) you could do the following:

  1. Use a callback in your cell, which gets called every time the textfield ends editing:

.

class MyTableViewCell: UITableViewCell {
var textHasChanged: ((String) -> Void)?
...
}
extension MyTableViewCell: UITextFieldDelegate {
// this gets called every time the user ends editing the text field
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
let newValue = textField.text //here you have your value

// now save it to your data source
self.textHasChanged(newValue)
}
}

  1. In a initializer or in awakeFromNib() function (depends on your usage), set the .delegate property of the textfield to self

  2. Now, to have each cell display the value from the datasource and to apply the changed text to your tableView datasource, add the following lines to your UITableViewController:

.

class MyTableViewController: UITableViewController {
var textArray: [String] = ["abc", "def"]
...
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)

cell.textField.text = textArray[indexPath.row]
cell.textHasChanged = { (newValue) in
self.textArray[indexPath.row] = newValue
}
return cell
}

Just comment if you have further questions

Cannot get text from UITextField - Swift - iOS

Check in Interface Builder that your UITextField is properly linked to your "userField" IBOutlet: your UITextField, self.userField, is probably not hooked to the IBOutlet, so the object is nil and it crashes when trying to access its .text property.

How to get value from UITextfield and sent it to email?

If you are wondering how to get the text from textfield, and send it, you will need to use textfield.text

NSString *emailBody = textField.text;
[mailer setMessageBody:emailBody isHTML:NO];

How to show entered data from UITextField to Label permanently?

Implement UITextFieldDelegate and set it to textField.delegate property. From UITextFieldDelegate implement shouldChangeCharactersIn callback method that gets called everytime the user tries to change input in the textfield:

class MyViewController: UIViewController {

...

func viewDidLoad() {
super.viewDidLoad()

// set the textField's delegate to self
textField.delegate = self
}

}

extension MyViewController: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// to be always updated, you cannot use textField.text directly, because after this method gets called, the textField.text will be changed
let newStringInTextField = (textField.text as NSString?)?.replacingCharacters(in: range, with: string)
yourLabel.text = "\(newStringInTextField) smthg"
return true
}
}

Using arguments of the function you can get a string that will appear in textField and you can set it as text in yourLabel.



Related Topics



Leave a reply



Submit