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:
- 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)
}
}
In a initializer or in
awakeFromNib()
function (depends on your usage), set the.delegate
property of the textfield toself
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
Swiftui: Using View Modifiers Between Different iOS Versions Without #Available
Swift Objc_Getassociatedobject Always Nil
How to Process an Array of Task Asynchronously with Swift Combine
Swift Alternative to Respondstoselector:
Swiftui: Navigationlink Not Working If Not in a List
How to Do a Long Press in Swift
How to Make Embedded View Controller Part of the Responder Chain
Getting a Segmentation Fault: 11 with Swift 5.2 When Using Filemanager.Default.Currentdirectorypath
Swiftui Scroll/List Scrolling Events
Com.Apple.Itunes Aedeterminepermissiontoautomatetarget Is Always Return -600
Swiftui - Pass Data to Different Views
Use Different Googleservice-Info.Plist for Single Project in Xcode Using Swift4
Swift "Is" Operator with Type Stored in Variable
Building for Arm64E on Apple Silicon
Passing and Storing Closures/Callbacks in Swift
Swift - How to Get Text Formatting in a Text Editor Like in the Notes App? Swiftui