Brian van den heuvel
Brian van den heuvel

Reputation: 396

Error with custom transition on loading UIView in Swift

I have UiViewcontroller which had multiple elements. When clicking the accessoryButtonType on a cell it should open a window with a custom transition, however after creating the code for the transition my UIViewController won't load at all.

by adding breakpoints it crashes after my viewDidLoad method;

here is the first part of my script of the viewController with its viewDidLoad Method

class jeans: UIViewController, UITableViewDelegate{

@IBOutlet var insertion: UITableView!
@IBOutlet var continent: UITableView!
@IBOutlet var sizeOutputCell: UITableView!
var size = ["Size waist", "Size inseam"]
let customTransitionManager = TransitionManager()

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = UIColor(red:(239/255.0), green:(238/255.0), blue:(243/255.0), alpha: 1.0)
    insertion.scrollEnabled = false;
    continent.scrollEnabled = false;


    let nib = UINib(nibName: "vwTableCell", bundle: nil)
    self.insertion.registerNib(nib, forCellReuseIdentifier: "customCell")
    let outputNib = UINib(nibName:"outputView", bundle:nil)
    self.sizeOutputCell.registerNib(outputNib, forCellReuseIdentifier: "outputCell")
    self.customTransitionManager.sourceViewController = self

}

This line of code gets highlighted in my TransitionManager.Class ( it is line 128):

let menuViewController = !self.presenting ? screens.from as! DetailedView : screens.to as! DetailedView

With the following error:

Could not cast value of type 'Converter.ViewController' (0x1000f7ce0) to 'Clothes_Converter.DetailedView' (0x1000f7f10).

this is the method it is in:

func animateTransition(transitionContext: UIViewControllerContextTransitioning) {

    // get reference to our fromView, toView and the container view that we should perform the transition in
    let container = transitionContext.containerView()

    // create a tuple of our screens
    let screens : (from:UIViewController, to:UIViewController) = (transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!, transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!)

    // assign references to our menu view controller and the 'bottom' view controller from the tuple
    // remember that our menuViewController will alternate between the from and to view controller depending if we're presenting or dismissing
    let menuViewController = !self.presenting ? screens.from as! DetailedView : screens.to as! DetailedView
    let topViewController = !self.presenting ? screens.to as UIViewController : screens.from as UIViewController

    let menuView = menuViewController.view
    let topView = topViewController.view

    // prepare menu items to slide in
    if (self.presenting){
        self.offStageMenuControllerInteractive(menuViewController) // offstage for interactive
    }

    // add the both views to our view controller

    container.addSubview(menuView)
    container.addSubview(topView)
    container.addSubview(self.statusBarBackground)

    let duration = self.transitionDuration(transitionContext)

    // perform the animation!
    UIView.animateWithDuration(duration, delay: 0.0, options: nil, animations: {

        if (self.presenting){
            self.onStageMenuController(menuViewController) // onstage items: slide in
            topView.transform = CGAffineTransformMakeTranslation(-container.frame.width, 0)
        }
        else {
            topView.transform = CGAffineTransformIdentity
            self.offStageMenuControllerInteractive(menuViewController)
        }

        }, completion: { finished in

            // tell our transitionContext object that we've finished animating
            if(transitionContext.transitionWasCancelled()){

                transitionContext.completeTransition(false)
                // bug: we have to manually add our 'to view' back http://openradar.appspot.com/radar?id=5320103646199808
                UIApplication.sharedApplication().keyWindow!.addSubview(screens.from.view)

            }
            else {

                transitionContext.completeTransition(true)
                // bug: we have to manually add our 'to view' back http://openradar.appspot.com/radar?id=5320103646199808
                UIApplication.sharedApplication().keyWindow!.addSubview(screens.to.view)

            }
            UIApplication.sharedApplication().keyWindow!.addSubview(self.statusBarBackground)

    })

}

How can i fix this so it will work? i have tried to narrow down where it crashes but adding a breakpoint in my viewDidLoad (at the end) and then step into everything made xCode crash (cuz its to much) i have also added breakpoints before each method.

if for any reason you need the whole code it is available on github~: https://github.com/bbriann123/Clothes/tree/master/Clothes_Converter/Clothes%20Converter

any help would be greatly appreciated.

Upvotes: 1

Views: 1839

Answers (1)

user3671459
user3671459

Reputation: 455

DetailedView is not a ViewController but you are forcing it to be one.

Change the ! in as! DetailedView to as? DetailedView and the rest should speak for themselves.

here's the code

func animateTransition(transitionContext: UIViewControllerContextTransitioning) {

    // get reference to our fromView, toView and the container view that we should perform the transition in
    let container = transitionContext.containerView()

    // create a tuple of our screens
    let screens : (from:UIViewController, to:UIViewController) = (transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!, transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!)

    // assign references to our menu view controller and the 'bottom' view controller from the tuple
    // remember that our menuViewController will alternate between the from and to view controller depending if we're presenting or dismissing
    //Changed the ! to ?
    let menuViewController = !self.presenting ? screens.from as? DetailedView : screens.to as? DetailedView
    let topViewController = !self.presenting ? screens.to as UIViewController : screens.from as UIViewController
    let menuView = menuViewController?.view
    let topView = topViewController.view
    // prepare menu items to slide in
    if (self.presenting){
        //self.offStageMenuControllerInteractive(menuViewController) // offstage for interactive
    }

    // add the both views to our view controller

    container.addSubview(menuView!)
    container.addSubview(topView)

    //THROWS unexpected nil value while unwrapping.
    //container.addSubview(self.statusBarBackground)

    let duration = self.transitionDuration(transitionContext)
    // perform the animation!
    UIView.animateWithDuration(duration, delay: 0.0, options: .CurveEaseInOut , animations: {
        if (self.presenting){
            self.onStageMenuController(menuViewController!) // onstage items: slide in
            topView.transform = CGAffineTransformMakeTranslation(-container.frame.width, 0)
        }
        else {
            topView.transform = CGAffineTransformIdentity
            self.offStageMenuControllerInteractive(menuViewController!)
        }

        }, completion: { finished in

            // tell our transitionContext object that we've finished animating
            if(transitionContext.transitionWasCancelled()){

                transitionContext.completeTransition(false)
                // bug: we have to manually add our 'to view' back http://openradar.appspot.com/radar?id=5320103646199808
                UIApplication.sharedApplication().keyWindow!.addSubview(screens.from.view)

            }
            else {

                transitionContext.completeTransition(true)
                // bug: we have to manually add our 'to view' back http://openradar.appspot.com/radar?id=5320103646199808
                UIApplication.sharedApplication().keyWindow!.addSubview(screens.to.view)

            }
            //THROWS unexpected nil value while unwrapping.
           // UIApplication.sharedApplication().keyWindow!.addSubview(self.statusBarBackground)

    })

}

Upvotes: 1

Related Questions