0

我有一个由motherViewController 控制的motherView,里面有一个容器视图。容器视图由 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
        }
    }
}

在我的母亲视图中有一个按钮。当这个按钮被触摸时,它会调用 MotherViewController 上的一个动作。

@IBAction func cancelButtonTapped(_ sender: UIBarButtonItem) {

       //call clean method of containerView instance

}

如何通过此操作调用特定 childView 实例上的 cleanTableView 函数?

4

2 回答 2

1

假设只有一个子视图控制器:

@IBAction func cancelButtonTapped(_ sender: UIBarButtonItem) {
    (children.first as? ChildViewController)?.clean()
}

有关 API 更改/重命名的一些附加信息:

childViewControllers属性已children在 Swift 4.2 中重命名为。请参阅https://developer.apple.com/documentation/uikit/uiviewcontroller/1621452-children?changes=latest_minor

重命名1 重命名2

于 2018-09-12T16:31:47.620 回答
1

有很多方法可以做到这一点,具体取决于组件的互连性以及您希望绑定它们的紧密程度。三个例子:

  • 紧密绑定:“母”VC 调用“子”VC 上的方法,后者调用子View 上的方法。

  • 与委托的松散绑定:创建委托协议,通过此委托将子 View 与母 VC 链接。然后,母 VC 调用委托。

  • 与通知断开连接:让子 View 监听特定的“清除”通知。让母亲 VC 发布该通知。两者之间没有直接联系。

每种方法的优缺点。最佳互动取决于您的具体情况。

于 2018-09-12T16:33:18.003 回答