我有一个由 motherViewController 控制的 motherView,其中有一个容器 View 。容器 View 由 childViewController 控制。 childView 包含一个 tableView。
现在我在 childViewController 中有一个 cleanTableView 函数,它在调用时“重置”tableView。
func clean() {
let indexPath = IndexPath(row: 0, section: 0)
if let cell = tableView.cellForRow(at: indexPath) {
if cell.accessoryType == .checkmark {
cell.accessoryType = .none
}
}
}
在我的 motherView 中有一个按钮。当触摸此按钮时,它会调用 motherViewController 上的操作。
@IBAction func cancelButtonTapped(_ sender: UIBarButtonItem) {
//call clean method of containerView instance
}
如何从此操作中调用特定 childView 实例上的 cleanTableView 函数?
最佳答案
假设只有一个 subview Controller :
@IBAction func cancelButtonTapped(_ sender: UIBarButtonItem) {
(children.first as? ChildViewController)?.clean()
}
有关 API 更改/重命名的一些附加信息:
在 Swift 4.2 中,childViewControllers
属性已重命名为 children
。请参阅https://developer.apple.com/documentation/uikit/uiviewcontroller/1621452-children?changes=latest_minor
关于ios - 从父 View Controller 调用 subview 实例的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52299702/