[英]Passing data between an object class and a ViewController (Swift)
[我想要什么]我想開發一個功能,當我點擊名為 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)
}
}
[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()
.
我希望你能幫助我解決這個問題。 提前致謝!
您可以為此使用委托或閉包
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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.