点击ParentViewController关闭ChildViewController

时间:2017-05-22 15:48:39

标签: ios swift

我有一个ViewController,它有一个添加按钮,可以在屏幕右侧打开SideBarVC。 如果我再次点击我的主ViewController屏幕它应该关闭我的SideBarVC。

我试过

@IBAction func click_leistung(_ sender: UIButton) {

    leistungList = self.storyboard?.instantiateViewController(withIdentifier: "leistungVC") as! leistungVC
    leistungList.view.backgroundColor = .clear
    leistungList.modalPresentationStyle = .overCurrentContext
         self.present(leistungList, animated: true, completion: nil)

}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {


    self.myscrollView.endEditing(false)
   leistungList.removeFromParentViewController()
    leistungList = nil
}

1 个答案:

答案 0 :(得分:0)

首先 - 您尝试删除新的VC,但是您应该将指针存储到旧的VC中以将其删除。创建var以存储指向此leistungList的指针,然后将其从父级中删除。

var storedController: UIViewController?
func show() {
    // ...
    storedController = presentedController
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    // Das keyboard ist weg
    self.myscrollView.endEditing(false)
    storedController.removeFromParentViewController()
    storedController = nil
}

PS:阅读本文以了解如何管理子控制器https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html