Hello guys I have a question about append a new element into an array everytime i pressed a button. I have two View Controllers, the first one "RegisterViewController", that contains a textfield and a button.
@IBAction func didTapSave(_ sender: UIButton) {
usernameText.endEditing(true)
userText = usernameText.text ?? ""
performSegue(withIdentifier: "goToData", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToData" {
let destinationVC = segue.destination as! UserTableViewController
destinationVC.userData = userText
destinationVC.saveItems()
}
The idea is to add the texfield.text to a tableView that is in the SecondViewcontroller
by far I can append one textfield.text, then when I go back to RegisterViewController and try again with a different text, the array that i use for the tableView just "edits" that last string but doesnt add another element.
When i print the itemArray.count it's always 0 that means there's only one text, but doesnt add the other ones if i continue writing on the textfield
class UserTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var userData : String?
var itemArray = [Item]()
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
saveItems()
}
func saveItems() {
let newItem = Item(userRegister: userData ?? "")
itemArray.append(newItem)
print(itemArray.count)
tableView.reloadData()
}
}