有了 MainViewController,如何在 MainVC 中与实际的 UIContainerViews 通信?
Having MainViewController, how to communicate with actual UIContainerViews within MainVC?
我有 MainViewController
有 4 UIContainerViews
.
根据显示的 ContainerView
,我在 MainViewController
上有不同的 UIBarButtonItem
。
当我点击UIBarButtonItem
时,我想与特定UIContainerView
的UIViewController
通信。
如何处理?
我试过在 child VC 上设置 static func
但没有成功。此外,self.storyboard?.instantiateViewController(withIdentifier
但最终,它正在实例化,而不是显示 VC...
在 interfaceBuilder 中为每个容器 segue 设置标识符。
在MainViewController中为每个容器定义一个占位符class:
class MainViewController: UIViewController {
var container1: UIViewController!
var container2: UIViewController!
var container3: UIViewController!
var container4: UIViewController!
```
}
- 为每个占位符分配
prepare(for segue...
函数内的实际值:
class MainViewController: UIViewController {
```
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let identifier = segue.identifier else { return }
switch identifier {
case "Segue1": container1 = segue.destination
case "Segue2": container2 = segue.destination
case "Segue3": container3 = segue.destination
case "Segue4": container4 = segue.destination
default: return
}
}
}
现在您可以分别访问每个视图控制器。
我有 MainViewController
有 4 UIContainerViews
.
根据显示的 ContainerView
,我在 MainViewController
上有不同的 UIBarButtonItem
。
当我点击UIBarButtonItem
时,我想与特定UIContainerView
的UIViewController
通信。
如何处理?
我试过在 child VC 上设置 static func
但没有成功。此外,self.storyboard?.instantiateViewController(withIdentifier
但最终,它正在实例化,而不是显示 VC...
在 interfaceBuilder 中为每个容器 segue 设置标识符。
在MainViewController中为每个容器定义一个占位符class:
class MainViewController: UIViewController {
var container1: UIViewController!
var container2: UIViewController!
var container3: UIViewController!
var container4: UIViewController!
```
}
- 为每个占位符分配
prepare(for segue...
函数内的实际值:
class MainViewController: UIViewController {
```
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let identifier = segue.identifier else { return }
switch identifier {
case "Segue1": container1 = segue.destination
case "Segue2": container2 = segue.destination
case "Segue3": container3 = segue.destination
case "Segue4": container4 = segue.destination
default: return
}
}
}
现在您可以分别访问每个视图控制器。