isaacmunoz
isaacmunoz

Reputation: 117

Passing data between an object class and a ViewController (Swift)

[WHAT I WANT] I want to develop a functionality that when I click on a button of an initial ViewController named BdaysVC (first image) a table view appears (second image) and when I click on a row of the tableView it goes back to the ViewController passing the activeSortingMode value. If I click on the opaque view it only dismisses the tableView without passing any value.

ViewController tableView

[WHAT I HAVE] The code that I have used to implement the tableView is:

class SortTableView: NSObject, UITableViewDelegate, UITableViewDataSource {

    ...

    func createViews (frame:CGRect) {
        // Creating an opaque view and a tableView and adding them above the BdaysVC
    }

    ...

    @objc func onClickTransparentView() {
        // Dismissing the tableView when touching the opaque view
    }

    ...

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        ...
        switch indexPath.row {
        case 0:
            // Change the activeSortingMode of BdaysVC to 0
        case 1:
            // Change the activeSortingMode of BdaysVC to 1
        case 2:
            // Change the activeSortingMode of BdaysVC to 2
        case 3:
            // Change the activeSortingMode of BdaysVC to 3
        default:
            print("sorting mode")
        }
    }

    ...

    override init() {
        super.init()
        ...
        tableView.delegate = self
        tableView.dataSource = self
        ...
    }

}

And for the ViewController:

class BdaysVC: UIViewController {
    ...
    var activeSortingMode: Int = 0
    ...
    let sortTableView = SortTableView()
    @IBAction func sortButtonClicked(_ sender: Any) {
        sortTableView.createViews(frame: self.view.frame)
    }

    override func viewWillAppear(_ animated: Bool) {
        ...
        retrieveSections(sortingMode: activeSortingMode)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        ...
        retrieveSections(sortingMode: activeSortingMode)
    }
}

[PROBLEM] My problem is that I don't know how to change the activeSortingMode value of the ViewController when a cell of the tableView is selected and reload the ViewController with the method viewWillAppear().

I hope you can help me with this issue. Thanks in advance!

Upvotes: 0

Views: 75

Answers (1)

Jawad Ali
Jawad Ali

Reputation: 14397

You can use delegate or closure for that

 protocol ActiveSortingModeSelect {
      func activeSortingModeSelected(_ index: Int)
    }


      class SortTableView: NSObject, UITableViewDelegate, UITableViewDataSource {

            ...
          weak var delegate: ActiveSortingModeSelect!

            func createViews (frame:CGRect) {
                // Creating an opaque view and a tableView and adding them above the BdaysVC
            }

            ...

            @objc func onClickTransparentView() {
                // Dismissing the tableView when touching the opaque view
            }

            ...

            func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
                ...
               delegate.activeSortingModeSelected(indexPath.row)

            ...

            override init() {
                super.init()
                ...
                tableView.delegate = self
                tableView.dataSource = self
                ...
            }

        }

    class BdaysVC: UIViewController {
        ...
        var activeSortingMode: Int = 0
        ...
        let sortTableView = SortTableView()
        @IBAction func sortButtonClicked(_ sender: Any) {
            sortTableView.createViews(frame: self.view.frame)
        }

        override func viewWillAppear(_ animated: Bool) {
            ...
            retrieveSections(sortingMode: activeSortingMode)
        }

        override func viewDidLoad() {
            super.viewDidLoad()
            ...
            sortTableView.delegate = self // Don't forget to set delegate 
            retrieveSections(sortingMode: activeSortingMode)
        }
    }
    extension BdaysVC: ActiveSortingModeSelect {
      func activeSortingModeSelected(_ index: Int) {
       self.activeSortingMode = index
      }
    }

Upvotes: 3

Related Questions