0

I am rather new to Xcode and Swift and I am trying to pass data back from a previous page. So far what I have works but it leverages the segue I created to show the previous page. This would not be a problem except it is in a navigation view controller. So when you click the save button it passes the data to my choices view controller but if you click the back button on the page it goes back to the editing page. I want it to instead travel back in the navigation pane so that way when you click the back page it instead takes it to the page that says view my chart. So I don't think using a segue to do this is the right option because I am typically passing the data forward. I want to pass the data back to the previous navigation view controller. You will see my cancel button does this.

Story Board

Edit ViewController

  class EditUserViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {

     var officeSelected = ""
     var utilSelected = ""

     @IBOutlet weak var officePicker: UIPickerView!

    @IBOutlet weak var slider: UISlider!
    @IBAction func UtilSlider(_ sender: UISlider) {
        slider.value = roundf(slider.value)
        utilSelected = String(Int(sender.value))
    }

    let offices = ["NYC", "LA", "Boston"]

    func numberOfComponents(in pickerView: UIPickerView) -> Int
    {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
    {
        return offices.count
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
    {
        return offices[row]
    }


    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
    {
        officeSelected = offices[row] 

    }


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func CancelEdit(_ sender: UIBarButtonItem) {
        self.dismiss(animated: true, completion: nil)
    }


    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let choicesView = segue.destination as! ChoicesViewController
        choicesView.officeText = officeSelected
        choicesView.utilText = utilSelected
    }
}

choices ViewController

    class ChoicesViewController: UIViewController{

     @IBOutlet weak var officeLabel: UILabel!

     @IBOutlet weak var AvalLabel: UILabel!

     var utilText = String()
     var officeText = String()

    override func viewDidLoad() {
        super.viewDidLoad()

        officeLabel.text = officeText
        AvalLabel.text = utilText

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
Robert
  • 3,790
  • 1
  • 27
  • 46
  • You can do this with closures or with delegates, its your choice – Reinier Melian Jun 21 '17 at 18:40
  • Would you mind providing an example? @ReinierMelian – Jacob Blacksten Jun 21 '17 at 18:42
  • 1
    You can always use self.navigationController.pushViewController and popViewController to handle navigation, also I think that you don't need a second navigationController on your storyboard – Reinier Melian Jun 21 '17 at 18:48
  • Possible duplicate of [Passing Data between View Controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – DonMag Jun 21 '17 at 19:04
  • There are many, many, many discussions and examples of how to do this. Surely a simple search would turn up something useful. – DonMag Jun 21 '17 at 19:05

0 Answers0