2017-08-08 90 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 
    } 
}