简体   繁体   中英

UITransitionContextFromViewControllerKey is returning nil

So, I am using the Elastic Transition pod (Cocoapods), and when I transition my app crashes because the UITransitionContextFromViewControllerKey key is nil . I am really confused as to why that value would be nil . What are some probable causes and solutions to resolving this error?

So here is some of my code for when I am transitioning into the next view controller:

func handleTap(sender: AnyObject) {
    transition.sticky = true
    transition.transformType = .TranslateMid
    transition.showShadow = true
    transition.edge = .Left
    transition.startingPoint = sender.center
    performSegueWithIdentifier("mySegue", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    segue.destinationViewController.transitioningDelegate = transition
    segue.destinationViewController.modalPresentationStyle = .Custom
}

Then, when it is trying to transition my app crashes complaining that this line of code (in Elastic Transition) is returning nil :

transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!

According to the ElasticTransition Github page , if a view controller is presented modally with elastic transition, then the destination view controller's transitioningDelegate needs to be set to ElasticTransition and modalPresentationStyle to .Custom . However, if the view controller is pushed onto a navigation controller stack using the elastic transition, then only the navigationController?.delegate needs to be set to ElasticTransition .

After some chatting, @Harish told me that he uses a push segue. However, the code in prepareForSegue is the setup code for when one presents a view controller modally . That is likely the reason why UITransitionContextFromViewControllerKey returns nil. So I believe the solution is to set the navigationController 's delegate to ElasticTransition somewhere, and remove the code in prepareForSegue .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM