我有一个包含多个容器视图的视图控制器,我们将其称为HomeViewController。我这样声明这些容器视图(childViewControllers)(每个容器视图都是一个具有自己的嵌入viewController的容器视图:
private lazy var startContactViewController: StartContactViewController = {
//Load Storyboard
let storybaord = UIStoryboard(name: "Main", bundle: Bundle.main)
//Instantiate View Controller
var viewController = storyboard?.instantiateViewController(withIdentifier: "StartContact") as! StartContactViewController
//Add View Controller as Child View Controller
self.add(asChildViewController: viewController)
return viewController
}()
private lazy var stopContactViewController: StopContactViewController = {
//Load Storyboard
let storybaord = UIStoryboard(name: "Main", bundle: Bundle.main)
//Instantiate View Controller
var viewController = storyboard?.instantiateViewController(withIdentifier: "StopContact") as! StopContactViewController
//Add View Controller as Child View Controller
self.add(asChildViewController: viewController)
return viewController
}()
private lazy var startDayViewController: StartDayViewController = {
//Load Storyboard
let storybaord = UIStoryboard(name: "Main", bundle: Bundle.main)
//Instantiate View Controller
var viewController = storyboard?.instantiateViewController(withIdentifier: "StartDay") as! StartDayViewController
//Add View Controller as Child View Controller
self.add(asChildViewController: viewController)
return viewController
}()
private lazy var loadingViewController: LoadingViewController = {
//Load Storyboard
let storybaord = UIStoryboard(name: "Main", bundle: Bundle.main)
//Instantiate View Controller
var viewController = storyboard?.instantiateViewController(withIdentifier: "loading") as! LoadingViewController
//Add View Controller as Child View Controller
self.add(asChildViewController: viewController)
return viewController
}()
然后我有以下两个功能来添加和删除childViewControllers:
func add(asChildViewController viewController: UIViewController) {
//Add Child View Controller
addChildViewController(viewController)
//Add Child View as Subview
view.addSubview(viewController.view)
//Configure Child View
viewController.view.frame = view.bounds
viewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
//Notify Child View Controller
viewController.didMove(toParentViewController: self)
}
func remove(asChildViewController viewController: UIViewController) {
// Notify Child View Controller
viewController.willMove(toParentViewController: nil)
// Remove Child View From Superview
viewController.view.removeFromSuperview()
// Notify Child View Controller
viewController.removeFromParentViewController()
}
我的应用首次加载时,将显示StartDayViewController。在此视图控制器中,它们是一个按钮。当用户按下该按钮时,我希望删除StartDayViewController并显示StartContactViewController。如何从StartDayViewController做到这一点?
我还包括了情节提要的图片。 Storyboard image
答案 0 :(得分:1)
您可以使用协议进行操作
protocol StartVcProtocol {
func startButtonPressed()
}
让HomeViewController实现它
extension HomeViewController: StartVcProtocol {
func startButtonPressed() {
// start button pressed -- do your remove and add stuff here
}
}
现在是StartVc
class StartVc: UIViewController {
var delegate: StartVcProtocol?
//inside you start button iBaction
delegate?.startButtonPressed()
}
然后懒惰地初始化StartVc
viewController.delegate = self
希望你能弄懂所有的东西。