在对象 class 和 ViewController (Swift) 之间传递数据

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

[我想要什么] 我想开发一个功能,当我点击一个名为 BdaysVC(第一张图片)的初始 ViewController 按钮时 table 视图出现(第二张图片),当我单击 table 视图的一行时,它会返回到 ViewController 并传递 activeSortingMode 值。如果我单击不透明视图,它只会关闭 table 视图而不传递任何值。

[我有什么] 我用来实现 tableView 的代码是:

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
        ...
    }

}

对于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)
    }
}

[问题] 我的问题是当 ViewController 的一个单元格时,我不知道如何更改 activeSortingMode 的值=18=] 被选中并使用 viewWillAppear().

方法重新加载 ViewController

希望你能帮我解决这个问题。提前致谢!

您可以为此使用委托或闭包

 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
      }
    }