自定义Segue Uncaught Exception抛出

时间:2015-12-22 20:43:44

标签: ios xcode swift segue

我正在使用segue,当应用程序运行时,我点击按钮就会崩溃并给我一个错误"Could not perform segue with identifier"。这是我控制我的segue的代码:

@IBAction func unwindToViewController (sender: UIStoryboardSegue){

}
let transitionManager = TransitionManager()

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    // this gets a reference to the screen that we're about to transition to
    let toViewController = segue.destinationViewController as UIViewController

    // instead of using the default transition animation, we'll ask
    // the segue to use our custom TransitionManager object to manage the transition animation
    toViewController.transitioningDelegate = self.transitionManager

}

我的代码会出现什么问题?我有一个完整的swift文件说明segue将做什么,这意味着自定义segue是必要的。

这是我的转换类,转换发生在: class TransitionManager:NSObject,UIViewControllerAnimatedTransitioning,UIViewControllerTransitioningDelegate {

// MARK: UIViewControllerAnimatedTransitioning protocol methods

// animate a change from one viewcontroller to another
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()
    let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
    let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!

    // set up from 2D transforms that we'll use in the animation
    let offScreenRight = CGAffineTransformMakeTranslation(container!.frame.width, 0)
    let offScreenLeft = CGAffineTransformMakeTranslation(container!.frame.width, 0)

    // start the toView to the right of the screen
    toView.transform = offScreenRight

    // add the both views to our view controller
    container!.addSubview(toView)
    container!.addSubview(fromView)

    // get the duration of the animation
    // DON'T just type '0.5s' -- the reason why won't make sense until the next post
    // but for now it's important to just follow this approach
    let duration = self.transitionDuration(transitionContext)

    // perform the animation!
    // for this example, just slid both fromView and toView to the left at the same time
    // meaning fromView is pushed off the screen and toView slides into view
    // we also use the block animation usingSpringWithDamping for a little bounce
    UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.8, options: [], animations: {

        fromView.transform = offScreenLeft
        toView.transform = CGAffineTransformIdentity

        }, completion: { finished in

            // tell our transitionContext object that we've finished animating
            transitionContext.completeTransition(true)

    })
}

// return how many seconds the transiton animation will take
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
    return 0.5
}

// MARK: UIViewControllerTransitioningDelegate protocol methods

// return the animataor when presenting a viewcontroller
// remmeber that an animator (or animation controller) is any object that aheres to the UIViewControllerAnimatedTransitioning protocol
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    return self
}

// return the animator used when dismissing from a viewcontroller
func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    return self
}

}

1 个答案:

答案 0 :(得分:0)

您可以尝试添加if语句来检查identifier

  1. 确保在main.storyboard的“属性”检查器中设置了标识符

  2. 更新了以下代码:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    
        if (segue.identifier == "YOUR_IDENTIFIER_HERE") {
    
            let toViewController = segue.destinationViewController as! UIViewController
            toViewController.transitioningDelegate = self.transitionManager
    
        }
    
    }
    
相关问题