2017-08-08 80 views
0

Error : "fatal error: unexpectedly found nil while unwrapping an Optional value" during transition between two view on same view controller轉換時致命錯誤

當我打電話Flip1函數它給了我上面的錯誤。

下面的代碼:

@IBAction func Flip(_ sender: AnyObject){ 
    UIView.transition(from: Back, to: Front, duration: 0.5, options: UIViewAnimationOptions.transitionFlipFromLeft, completion: nil) 
} 

@IBAction func Flip1(_ sender: AnyObject){ 
    UIView.transition(from: Front, to: Back, duration: 1, options: UIViewAnimationOptions.transitionFlipFromRight, completion: nil) 
} 
+0

後視圖或前視圖必須爲零。您是使用故事板中的還是什麼? –

+0

@GarimaSaini yes故事板 –

+0

您是否檢查過您的網點'Front'和'Back'是否分配在故事板中? – clemens

回答

2

是的,我發現這個錯誤終於。

只是要出口強引用,而不是或弱,如:

@IBOutlet var Front: UIView! 
@IBOutlet var Back: UIView! 

就這麼幹!

+0

是的。有效。 –

1

這裏是一個回答您的問題:transitionFromView:toView:duration:options:completion:

Parameters (According to Documentation):

fromView
The starting view for the transition. By default, this view is removed from its superview as part of the transition.

toView
The ending view for the transition. By default, this view is added to the superview of fromView as part of the transition.

解決問題的方法:

使用transitionWithView:duration:options:animations:completion:而不是transitionFromView:toView:duration:options:completion:,如果你想既記憶中的觀點。

試試這個:

  1. 添加含有 '前' 和 '後退' 視圖一個UIView。 (添加一個新視圖作爲「前」和「後」視圖的超級視圖)

  2. 在「超級視圖」上執行翻轉動畫,同時在「前」和「後」視圖上隱藏和取消隱藏操作。與transitionWithView:duration:options:animations:completion:動畫塊

示例代碼:

@IBOutlet var superView: UIView! 
@IBOutlet var Back: UIView! 
@IBOutlet var Front: UIView! 

@IBAction func Flip(_ sender: AnyObject){ 

    self.Front.isHidden = false 
    self.Front.alpha = 0.1 
    UIView.transition(with: superView, duration: 0.5, options: .transitionFlipFromLeft, animations: { 
     self.Back.alpha = 0.1 
     self.Front.alpha = 1.0 
    }) { (isCompleted) in 
     self.Back.isHidden = true 
    } 
} 

@IBAction func Flip1(_ sender: AnyObject){ 

    self.Back.isHidden = false 
    self.Back.alpha = 0.1 
    UIView.transition(with: superView, duration: 0.5, options: .transitionFlipFromRight, animations: { 
     self.Front.alpha = 0.1 
     self.Back.alpha = 1.0 
    }) { (isCompleted) in 
     self.Front.isHidden = true 
    } 
}