ios - 从父 View Controller 调用 subview 实例的函数

标签 ios swift childviewcontroller parentviewcontroller

我有一个由 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

renaming1 renaming2

关于ios - 从父 View Controller 调用 subview 实例的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52299702/

相关文章:

iphone - 如何将 UIWebView 移出屏幕?

css - 宽度 100% 不适用于 iOS

ios - 下载图像,然后从 Firebase Swift 3 缓存它

swift - “ fatal error :在展开可选值时意外发现nil”是什么意思?

ios - 为什么平移让用户将缩放 View 移到 super View 之外?

ios - 通过数组循环下载图像时的内存问题

ios - SFSpeechRecognizer 多种语言

ios - 尝试在 UIView 中添加 Tableview。我在这里做错了什么?

swift - 如何调整 subview Controller 的高度以匹配容器 View 的高度

ios - 如何以编程方式添加容器 View