-2

I want to send data from TextField from second view controller to first view controller and add this data to an array

I have a struct which I will save to array:

struct ContactsModel {
    var name : String
    var surname : String
    var phoneNumber : String
}

first VC:

class FirstViewController: UIViewController {  
    var contacts : [ContactsModel] = []
}

second VC:

class SecondViewController: UIViewController {

    @IBOutlet weak var nameTextField: UITextField!
    @IBOutlet weak var surnameTextField: UITextField!
    @IBOutlet weak var phoneNumberTextField: UITextField!

 @IBAction func saveAndClose(_ sender: UIButton) {
// here i want to send this objects (nameTextField, surnameTextField, phoneNumberTextField) in array in first VC when i press this button
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
kov_24
  • 1
  • 1

2 Answers2

0

You can accomplish this using a delegate:

struct ContactsModel {
    var name : String
    var surname : String
    var phoneNumber : String
}


protocol SecondViewControllerDelegate: class {
    func savedAndClosed(with model: ContactsModel)
}

class FirstViewController: UIViewController {
    var contacts : [ContactsModel] = []

    // Whereever you create and present your instance of SecondViewController make it conform to the delegate
    func showSecondVC() {
        let secondVC = SecondViewController()
        secondVC.delegate = self
        present(secondVC, animated: true, completion: nil)
    }
}

extension FirstViewController: SecondViewControllerDelegate {
    func savedAndClosed(with model: ContactsModel) {
        contacts.append(model)
    }
}

class SecondViewController: UIViewController {

    @IBOutlet weak var nameTextField: UITextField!
    @IBOutlet weak var surnameTextField: UITextField!
    @IBOutlet weak var phoneNumberTextField: UITextField!

    weak var delegate: SecondViewControllerDelegate?

    @IBAction func saveAndClose(_ sender: UIButton) {
        // here i want to send this objects (nameTextField, surnameTextField, phoneNumberTextField) in array in first VC when i press this button
        guard let name = nameTextField.text, let surname = surnameTextField.text, let phoneNumber = phoneNumberTextField.text else { return }
        let new = ContactsModel(name: name, surname: surname, phoneNumber: phoneNumber)
        delegate?.savedAndClosed(with: new)
    }
}
Pierce
  • 3,148
  • 16
  • 38
0

First be sure to make var contacts in FirstViewController static:

class FirstViewController: UIViewController {
    static var contacts : [ContactsModel] = []
}

Then in SecondViewController you can edit variable "contacts" like this:

class SecondViewController: UIViewController {

    @IBOutlet weak var nameTextField: UITextField!
    @IBOutlet weak var surnameTextField: UITextField!
    @IBOutlet weak var phoneNumberTextField: UITextField!

    @IBAction func saveAndClose(_ sender: UIButton) {
    // here i want to send this objects (nameTextField, surnameTextField, phoneNumberTextField) in array in first VC when i press this button
        FirstViewController.contacts.append(ContactsModel(name: nameTextField.text ?? "defaultName", surname: surnameTextField.text ?? "defaultSurname", phoneNumber: phoneNumberTextField.text ?? "defaultPhone"))
    }
}

You need to define default values so even if text from field would be nil your app won't crush, in example we set default values here:

name: nameTextField.text ?? "defaultName"
Maks
  • 302
  • 1
  • 2
  • 10