【问题标题】:Passing data between an object class and a ViewController (Swift)在对象类和 ViewController (Swift) 之间传递数据
【发布时间】:2020-08-23 11:36:31
【问题描述】:

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

[我有什么]我用来实现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 987654329 ViewController 987654330 @的tableView @ 987654330 ViewController 987654331 @ 使用方法viewWillAppear()

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

【问题讨论】:

  • 你可以使用委托或闭包
  • @jawadAli 我已经阅读了关于使用委托的信息,但我对如何在我的案例中实现它一无所知。我怎么能做到?谢谢!
  • 我已经更新了我的代码......如果您遇到任何困难,请告诉我
  • 有效吗?

标签: ios swift xcode uitableview uiviewcontroller


【解决方案1】:

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

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

【讨论】:

  • 抱歉回复晚了。它完全按照我想要的方式工作!谢谢!
猜你喜欢
  • 2019-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-24
  • 2017-01-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多