Swift構文に変換されたrdelmarの回答
override func viewDidLoad() {
super.viewDidLoad()
self.initialVC = self.childViewControllers.last
self.substituteVC = self.storyboard!.instantiateViewControllerWithIdentifier("Substitute")
self.currentVC = self.initialVC;
}
@IBAction func switchControllers(sender: UISegmentedControl) {
switch sender.selectedSegmentIndex{
case 0:
if (self.currentVC == self.substituteVC) {
self.addChildViewController(self.initialVC)
//self.initialVC.view.frame = self.containerView.bounds
moveToNewController(self.initialVC)
}
case 1:
if (self.currentVC == self.initialVC) {
self.addChildViewController(self.substituteVC)
//self.substituteVC.view.frame = self.containerView.bounds
moveToNewController(self.substituteVC)
}
default:
break;
}
}
func moveToNewController(newController : UIViewController){
self.currentVC.willMoveToParentViewController(nil)
self.transitionFromViewController(
self.currentVC!,
toViewController: newController,
duration: 0.2,
options: UIViewAnimationOptions.TransitionCrossDissolve,
animations: nil,
completion: { finished in
self.currentVC.removeFromParentViewController()
newController.didMoveToParentViewController(self)
self.currentVC = newController
})
}