我遇到了将子视图控制器“ B”呈现到另一个视图控制器“ A”的情况。视图控制器“ B”具有5个按钮,可将按钮锁定(按入)到第三个视图控制器“ C”。问题是一旦我进入视图控制器“ C”,我希望能够使用展开命令从C-> A转到中间,而不会出现B。
// view controller A class
class AViewController: UIViewController {
@IBAction func goToViewControllerB(_ sender: UIButton) {
let viewControllerB = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "viewControllerB_ID") as! BViewController
self.addChild(viewControllerB)
viewControllerB.view.frame = self.view.frame
self.view.addSubview(viewControllerB.view)
viewControllerB.didMove(toParent: self)
}
@IBAction func unwindToStart(segue: UIStoryboardSegue) {
print("back from view controller C!")
}
}
//view controller B class
class BViewController: UIViewController {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let identifier = segue.identifier {
switch identifier {
case "segueFromBToC_1":
if let destVC = segue.destination as? CViewController {
//pass data
}
// ... cases 2-4
case "segueFromBToC_5":
if let destVC = segue.destination as? CViewController {
//pass data
}
default: break
}
}
}
}
// empty CViewController class
我知道将以下代码放入我的视图控制器B类中会将其从父视图控制器“ A”中删除,但是由于“ B”不存在,我无法再将其从“ B”选择为“ C” 。
self.view.removeFromSuperview()
self.removeFromParent()
self.willMove(toParent: nil)
I was wondering where I should put the above code or if I should segue from a child view controller at all? I also haven't used navigation controllers as I don't know how to implement them with a child view controller. Should I have @IBAction
outlets for each of the 5 buttons and put the above code in there?
UPDATE: I was able to fix it by detaching the 5 push segues from the buttons and have each of the 5 push segues go directly from view controller B to view controller C. I then had an
@IBAction
for each button where I had the following code in view controller B:不确定这是否是最佳做法。