0
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        let storyboard = UIStoryboard(name: "FilteringPageSelection", bundle: nil)
        controller = storyboard.instantiateViewControllerWithIdentifier("FilteringPageSelectionController") as! FilteringPageSelectionTableViewController
        controller.filteringType = filterTitle[indexPath.row];
        controller.selectedValue = selectedValue;

        controller.title = "Selection"
        self.navigationController?.pushViewController(controller, animated: true)

        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }

Above is my code, this is my table view controller code, i am intend to push to another view controller by clicking it cell. Inside the new controller, there is multiple value for me to choose. however , after i select the new value from the new controller, i back to the original table view scene and get the selected value from the new controller and assign into the variable selectedValue. Below is my code of the unwind segue.

 @IBAction func unwindToFilteringVC(segue:UIStoryboardSegue) {
    selectedValue = controller.selectedValue
}

However, when i click the cell again, since the tableview override function only initialize for the first time when the tableview is loaded, so when i change the value of the selectedValue, the controller.selectedValue still remain the old value and push into new controller.

How do i changes the value of controller.selectedValue?

Ryan Shine
  • 442
  • 1
  • 9
  • 23

2 Answers2

0

I think what you need to use is a delegate pattern to inform the table view controller of the selected value in the selection view controller instead of holding a reference to it. First you define a protocol:

protocol SelectionViewControllerDelegate: class {
  func selectionViewController(controller: SelectionViewController, didSelectValue value: ValueType)
}

You add a weak reference to an optional property that conforms to that protocol and you inform the delegate of changes in the selected value right where you handle the selection action in your selection view controller.

class SelectionViewController: UIViewController {
  weak var delegate: SelectionViewControllerDelegate?

  func handleChange() {
    delegate?.selectionViewController(self, didSelectValue: selectedValue)
  }
}

Then you make your table view controller conform to this protocol like so:

class TableViewController: UITableViewController, SelectionViewControllerDelegate {

  func selectionViewController(controller: SelectionViewController, didSelectValue value: ValueType) {
    self.selectedValue = value
  }

}

Don't forget to set the table view controller as the delegate of the selection view controller in the didSelectRowAtIndexPath method by saying:

controller.delegate = self

Right before you call navigationController?.pushViewController method.

Ahmed Onawale
  • 3,992
  • 1
  • 17
  • 21
0

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) - this function gets called every time you tap on a cell, not just once. So whenever you select a cell, your code instantiates the filter view controller and sets its selectedValue to the current selectedValue of the table view controller (unless that is a global variable). Maybe that is not what you want.

OR

You may have mixed which selectedValue is which. You should rename one.

Beirs
  • 538
  • 4
  • 7