【发布时间】:2020-12-22 18:33:37
【问题描述】:
我在 Swift 中工作,并且没有调用函数 categoryPressedFunction。
协议:
protocol categoryPressed: class { //1 create a protocol with function that passes a string
func categoryPressedFunction(category: String)
}
查看控制器 2:
//set the delegate
weak var delegate: categoryPressed?
//when cell is selected in tableview, grab the "category" which is a string and then dismiss
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let category = guideCategoryArray[indexPath.row] // gets category
delegate?.categoryPressedFunction(category: category) // sets delegate to know it was pressed
self.presentingViewController?.dismiss(animated: true, completion: nil) //dismisses
}
}
查看控制器 1(上一个)
//set class delegate
...categoryPressed
//add function that sets label to category and looks up items based off category
func categoryPressedFunction(category: String) {
print("categoryPressedFunctionPressed")
resourceArray.removeAll()
resourceLabel.text = category
getItems(item: category, { [self] in
print("got new items for \(category) and refreshed the tableview")
self.resourceTableView.reloadData()
})
}
返回 ViewController 1 时,什么也没有发生。表格视图不会重新加载,标签也不会更改为按下的类别。我错过了什么吗?
【问题讨论】:
-
请注意:协议应大写,如
protocol CategoryPressed: class {