1

I have a dictionary that I want to manipulate depending on options selcted by the user.

in the first View Controller:

var filterDictionary = [String : [String]]()

includes a function which is called from the second View Controller

func onOptionsCLosed(_ filter : String, options : [String]) {

    if options.count > 0 {
        print(filter)
        print(options)
        filterDictionary[filter] = options
        print(filterDictionary)
    }
}

and also an action that I'm using to test the result

@IBAction func submitFilter(_ sender: UIButton) {

    print(filterDictionary)

}

in the second View Controller, I have an action that checks the items in an array to make sure there something there and then passes the needed values to the first View Controller

 @IBAction func submitOptions(_ sender: UIButton) {
    let mainVC: FilterViewController = FilterViewController()
    var selectedOptions: [String]? = []

    if (optionCollectionView.indexPathsForSelectedItems != nil) {
        for i in optionCollectionView.indexPathsForSelectedItems! {
            let pos = i[1]
            selectedOptions?.append(options[pos])
        }

        mainVC.onOptionsCLosed(filter, options: selectedOptions!)
    } else {

        print("ELSE")
        // no items were selected prompt
    }

    _ = self.navigationController?.popViewController(animated: true)
}

The problem I'm running into is once the correct parameters are passed to the first View Controller and I'm able to print them out but if I click the button to check the values contained by the dictionary it prints and empty dictionary or rather it'll print: [:]

rmaddy
  • 314,917
  • 42
  • 532
  • 579

1 Answers1

1

The problem is that you're not passing the values to the first view controller. You're actually creating a third view controller in this line:

let mainVC: FilterViewController = FilterViewController()

You should grab a reference to the existing first view controller using self.presentingViewController or by setting a reference manually on SecondViewController before presenting.

Chris Allwein
  • 2,498
  • 1
  • 20
  • 24