2015-12-22 52 views
-2

我使用的是segue,當應用程序運行時,我點擊按鈕它崩潰,並給我一個錯誤,說"Could not perform segue with identifier"。這是我控制我的segue與代碼:自定義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 

} 

我的代碼會這樣做會有什麼錯?我有一個快速的文件說明segue會做什麼,這意味着自定義segue是必要的。

這是我的過渡類,其中的過渡情況: 類的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

那不是你真正的代碼。顯示你的真實代碼。 – matt

+0

也解釋如何配置按鈕。 – matt

+0

不要在這裏修改你的代碼或刪除其中的重要部分,從IDE複製並粘貼它。 – luk2302

回答

0

你可以嘗試添加if語句來檢查identifier

  1. 確保你有一個標識符在屬性檢查器在下面

  2. 更新代碼main.storyboard設置:

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

我的segue沒有設置標識符。應該有一個? –

+0

@ iap-Software我會親自嘗試一下。你知道在哪裏設置標識符嗎? – Nick89

+0

我知道在哪裏設置它,但我不知道標識符是什麼。你知道這是什麼嗎? –