简体   繁体   中英

How can I send data back to the first view controller after detecting that the back button was pressed?

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.

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.

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)
      }
   }

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM