我有一个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
}发布于 2017-05-22 23:55:23
首先,你试图移除新的VC,但你应该存储指向旧VC的指针来移除它。创建var来存储指向此leistungList的指针,然后将其从parent中删除。
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
https://stackoverflow.com/questions/44117126
复制相似问题