我有一个容器嵌入在带有导航栏的 ViewController 中。 ContainerView 包含另一个带有一些文本字段的 ViewController。
当 ContainerView 首次显示时,文本字段被禁用。
我想做的是在导航栏中添加一个编辑按钮,以启用文本字段。
基本上, View Controller 中的条形按钮项目是否可以对容器中显示的另一个 View Controller 执行操作?
最佳答案
在这种情况下你可以做一些小技巧
条形按钮将在 ViewController 中,但我们可以将 ContainerView 的内容存储在第一个 ViewController 的变量中
为此,我建议让 ContainerView 的内容有一个自定义类,这样您就可以在其中编写一个函数
这里是一个关于如何捕获ContainerView的内容的例子
class ViewController: UIViewController{
weak var containter: ContainerViewController!
@IBAction func menuAction(_ sender: UIBarButtonItem) {
//do what you want containter.doAction()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if let vc = segue.destination as? ContainerViewController {
self.containter = vc
}
}
}
关于ios - ViewContainer 中 ViewController 上的条形按钮操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40119588/