-2

I want make a filter for search (on a separate screen) and I think about how to transfer data for my second viewcontroller. Is there a way to use something like pointers from C++? When all changes in my array (at second view) change in the first view, that is, in fact I do not need to copy data because I use a pointer. If I can't use that how can I transfer my array after change back at first view?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 2
    See https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers?rq=1 – rmaddy Mar 31 '18 at 22:14
  • Possible duplicate of [Passing Data between View Controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Jakub Truhlář Apr 01 '18 at 19:49

3 Answers3

1

don't use pointer, transfer you array by NotificationCenter.

JIE WANG
  • 1,875
  • 1
  • 17
  • 27
1

Use Delegation to pass data back to previous class,

From First Class,

class FirstClass: UIViewController, SecondClassDelegate {

@IBAction func goToSecondClass(_ sender: Any) {
    performSegue(withIdentifier: "SecondClassId", sender: nil)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let destination = segue.destination as? SecondClass {
        destination.delegate = self
    }
}

func finishPassing(stringPassed: String) {
   print(stringPassed)
}

}

In Second Class,

protocol SecondClassDelegate {
    func finishPassing(string: String)
}

class SecondClass: UIViewController {
@IBAction func btnPassDataPressed(_ sender: Any) {
    var delegate: SecondClassDelegate?
    delegate?.finishPassing(string: "Data passed from Second Class")
  }
}
Bappaditya
  • 9,494
  • 3
  • 20
  • 29
1

Three ways here:-

If you have to pass data to previous controller then use Custom Delegation.

If you have to pass data to next controller then, just assign data to next view controller variable.

You can get data by saving it in Userdafults.

Manish Mahajan
  • 2,062
  • 1
  • 13
  • 19