我有一个包含2个索引的segmentControl。它位于父视图控制器内。父视图控制器有2个containerView,每个都有一个嵌入的viewController。
parentVC
|
segmentedControll _vcZero
| /
2containerVCs---------
\_vcOne
在vcZero和vcOne的viewWillAppear方法中,我有一些打印语句。
问题是当parentVC出现在场景上并且selectedSegmentIndex = 0显示时,它的print语句会运行,但vcOne中的print语句也会运行。考虑到vcOne不在场,它的代码不应该运行。
据我所知,当parentVC加载时,两个containerView都被加载到内存中。我尝试了这2个SO答案,但都没有阻止vcOne的打印语句打印。
@IBOutlet weak var segmentedControl: UISegmentedControl!
@IBOutlet weak var vcZeroContainer: UIView!
@IBOutlet weak var vcOneContainer: UIView!
let viewControllerIdentifiers = ["vcZeroContainer", "vcOneContainer"]
@IBAction func segmentChanged(sender: UISegmentedControl) {
var newController = storyboard?.instantiateViewControllerWithIdentifier(viewControllerIdentifiers[sender.selectedSegmentIndex]) as UIViewController
let oldController = childViewControllers.last as UIViewController
oldController.willMoveToParentViewController(nil)
addChildViewController(newController)
newController.view.frame = oldController.view.frame
transitionFromViewController(oldController, toViewController: newController, duration: 0.25, options: .TransitionCrossDissolve, animations:{ () -> Void in
// nothing needed here
}, completion: { (finished) -> Void in
oldController.removeFromParentViewController()
newController.didMoveToParentViewController(self)
})
}
和
@IBAction func segmentChanged(sender: UISegmentedControl) {
switch segmentedControl.selectedSegmentIndex
{
case 0:
vcOneContainer?.removeFromSuperview()
vcOneContainer = nil
case 1:
vcZeroContainer?.removeFromSuperview()
vcZeroContainer = nil
default:
break;
}
}
当selectedSegmentIndex = 0显示时,如何从内存中删除vcOneContainer?