-1

Now Using the Delegate Pattern to transfer the data:

I have implemented this function:

 var overviewController = addOverview()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    overviewController.delegate = self
}

func setValues(){
    overview.name = buildingName.text!
    overview.city = city.text!
    overview.contact = contactName.text!
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "MM-dd-yyyy"
    overview.date = dateFormatter.string(from: datePicker.date)
    overview.email = email.text!
    overview.building = buildingTypes[buildingPicker.selectedRow(inComponent: 0)]
    overview.freq = cleaningFreqs[cleaningFreqPicker.selectedRow(inComponent: 0)]
    overview.phone = phoneNumber.text!
    overview.sft = sqFt.text!
    overview.state = State.text!
    overview.street = street.text!
}



// MARK: - Navigation

override func willMove(toParent parent: UIViewController?) {
    super.willMove(toParent:parent)
    if parent == nil {
        setValues()
        print("Hello")
        delegate?.setValues(obj: overview)
    }
}

And here i the setValues from the protocol I wrote:

func setValues(obj: overviewProps){
    overview = obj
}

However after printing one of the properties of the object, the data has not been transferred.

DRaff
  • 13
  • 4

3 Answers3

1

There are many ways to pass/exchange data between two viewControllers. I do not know how your implementation look like. But you can use delegate pattern to achieve your target. Use a delegate and trigger the methods when you want to pass data.

As I said there are other solutions, too. But it is up to you, which one you want to choose.

shuvo
  • 705
  • 5
  • 15
  • What can I add to my post to help you and others understand it? – DRaff Mar 19 '19 at 01:24
  • You can just implement delegate pattern in your code. It does not depend on how you implement your code, it will work. And delegate pattern is very important in iOS development. You have to use it eventually. I've found this blog, https://learnappmaking.com/pass-data-between-view-controllers-swift-how-to/ , Here, the author mentioned various ways(both forward and backward data passing, including delegate pattern), this will be very helpful for you to understand. – shuvo Mar 19 '19 at 04:41
  • I added the delegate pattern using that link, thank you! However the code is still not working as intended because the data is not being passed back. I have updated my code to show this – DRaff Mar 19 '19 at 12:32
1

You can use Delegate pattern or use Observer. See these examples for help.

Edit: using delegate based on your code

Protocol

protocol SetValueDelegate {
    func didFinishSetValue(obj: Overview)
}

First view controller

class FirstViewController: UIViewController, SetValueDelegate {

   var secondViewController = SecondViewContrller()

   override func viewDidLoad() {
       super.viewDidLoad()
       secondViewController?.delegate = self
   }

   func didFinishSetValue(obj: Overview) {
       // when it comes back to the first screen you can use ->> obj data
   }

}

Second view controller

class SecondViewController: UIViewController {
   var delegate: SetValueDelegate?

   override func viewDidLoad() {
       super.viewDidLoad()
       …
   }
   override func willMove(toParent parent: UIViewController?) {
      super.willMove(toParent: parent)
      if parent == nil {
         setValues()
         …
         self.delegate?.didFinishSetValue(obj: overview)
      }
   }

}
Chhaileng
  • 2,428
  • 1
  • 27
  • 24
  • I implemented the delegate protocol, and the data is still not being saved back to the initial view controller. However I tested that that code is running when the back button is pressed. – DRaff Mar 19 '19 at 12:26
  • I think you case is similar to this project, so you can follow this: https://github.com/chhaileng/LoginDelegeteHomework – Chhaileng Mar 19 '19 at 12:32
  • So I actually use a segue to go to the view, should I present it as you did in the code? – DRaff Mar 19 '19 at 12:40
  • I've just updated my answer u can check and try it. – Chhaileng Mar 19 '19 at 12:57
  • Okay, I've pinpointed the issue and the issue is that method listed in the protocol is not executing – DRaff Mar 19 '19 at 13:28
  • When you are using delegate don’t forget to initiate or set the ‘delegate’ object. (SecondVC.delegate = self) the method won’t work if u forget this. – Chhaileng Mar 19 '19 at 13:30
  • I did set it just like you did in the example – DRaff Mar 19 '19 at 13:45
  • updating my post – DRaff Mar 19 '19 at 13:45
0

The proper way to pass data between the current view and the root view with your navigation design is to trigger an unwind segue.

Have a look at the follow article. It is in Swift 3, but it should give you a good start in translating that into Swift 4.2

https://link.medium.com/b4eFIx7LaV

Tal Zion
  • 6,308
  • 3
  • 50
  • 73