0

Thanks in advance for the help!

I'm trying to use segmented control to change child views inside a container view on one of my view controllers. I found a good answer on SO about this and below was the following code:

@IBAction func segmentedControlChange(sender: AnyObject) {

    var newController = storyboard?.instantiateViewControllerWithIdentifier(viewControllerIdentfiers[sender.selectedIndex]) 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: UIViewAnimationOptions.TransitionFlipFromRight, animations: {
        () -> Void in
        //nothing needed here
        }, completion: { (finished) -> Void in
            oldController.removeFromParentViewController()
            newController.didMoveToParentViewController(self)

    })
}

When I run it in the simulator and then click on the segmented control, I come up with THREAD 1: EXC_BAD_INSTRUCTION(code = EXC_l386_INVOP, subcode=0x0). I used story board identifiers on the view controllers as well.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Kody R.
  • 2,430
  • 5
  • 22
  • 42
  • I would suggest you post the entire message from the error console. The console will tell you `where your problem lives`. Look for references to your class and it'll list the method that's causing the crash. It'll also have stuff in there like `UIKit` methods. – Adrian Jul 09 '15 at 18:57

1 Answers1

0

Try to get your storyboard

let storyboard = UIStoryboard(name: "Main", bundle: nil)
Max
  • 1,143
  • 7
  • 7
  • I actually did that right after I posted this! It still didn't work. I double checked the storyboard IDs, and they are correct as well. – Kody R. Jul 09 '15 at 19:43
  • When you load your first view controller in Storyboard are you using an embed segue with your container view? If you are starting with a view controller that has nothing in the container view then it will not have any childViewControllers. This means that `childViewControllers.last` is nil but you are force unwrapping/casting it with `as!` which will cause this error. – Max Jul 09 '15 at 21:51