To pass the settings back to your first viewController, you can use a closure that is passed from the first viewController.
Here is an example of how to do this. In prepare(for:)
set the closure processTipSettings
to point to a handler function. In this case, changeSegmentLabels
will take the tip settings and make them the labels of the segmented control.
When the user hits save, the @IBAction for the button will call processTipSettings
with the values from the textFieldLabels, and changeSegmentLabels
will update the titles in the segmented control.
ViewController.swift:
import UIKit
class ViewController: UIViewController {
@IBOutlet var tipsSegmentedController: UISegmentedControl!
func changeSegmentLabels(labels: [String?]) {
if labels.count == 3 {
for i in 0..<3 {
tipsSegmentedController.setTitle(labels[i], forSegmentAt: i)
}
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "settings" {
if let dvc = segue.destination as? SettingsViewController {
dvc.processTipSettings = changeSegmentLabels
}
}
}
}
SettingsViewController.swift
class SettingsViewController: UIViewController {
@IBOutlet weak var tip1: UITextField!
@IBOutlet weak var tip2: UITextField!
@IBOutlet weak var tip3: UITextField!
// Call this closure when Save is pressed.
var processTipSettings: (([String?]) -> ())?
@IBAction func saveSettings(_ sender: UIButton) {
processTipSettings?([tip1.text, tip2.text, tip3.text])
_ = self.navigationController?.popViewController(animated: true)
}
}