翻转卡在swift中导致致命错误

时间:2016-03-24 01:42:34

标签: swift fatal-error

我正在关注this card flipping tutorial,而不是以编程方式创建UIViewUIImageViews我在故事板中创建了这些。
当我点击卡片时,动画开始并转到前面的图像,但当我再次点击时,会创建一个fatal error

  

在解包可选值时意外地发现了nil。

我无法弄清楚为什么会出现这种错误。

这是代码,它是上述教程的转换:

class FirstViewController: UIViewController {

@IBOutlet weak var flashCardView: UIView!
@IBOutlet weak var backImage: UIImageView!
@IBOutlet weak var frontImage: UIImageView!


var showingBack = true

    override func viewDidLoad() {
        super.viewDidLoad()

        let singleTap = UITapGestureRecognizer(target: self, action: Selector("tapped"))
        singleTap.numberOfTapsRequired = 1

        flashCardView.addGestureRecognizer(singleTap)
        flashCardView.addSubview(backImage)
        view.addSubview(flashCardView)
    }

    func tapped() {
        if (showingBack) {
            UIView.transitionFromView(backImage, toView: frontImage, duration: 1, options: UIViewAnimationOptions.TransitionFlipFromRight, completion: nil)
            showingBack = false
        } else {
            UIView.transitionFromView(frontImage, toView: backImage, duration: 1, options: UIViewAnimationOptions.TransitionFlipFromLeft, completion: nil)  
            //here I get the error when flipping the card back to the back image
            showingBack = true
        }
    }
} 

1 个答案:

答案 0 :(得分:0)

我相信当你转换时,旧图像的引用计数会减少,并且由于它们被声明为弱,它会被破坏。尝试删除弱声明,它可能会工作。看一下教程,他们不会有弱点。