当 UIButton 调用其选择器操作时获取 EXC_BAD_ACCESS

Getting EXC_BAD_ACCESS when UIButton calls its selector action

我有这段代码,我正在尝试为视图中的 UIButton 分配一个动作,最终目标是让它执行传递的闭包。但是,当我点击按钮并尝试调用分配给它的操作时,应用程序不正常地崩溃了。

特别是它显示了带有错误 Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)class AppDelegate: UIResponder, UIApplicationDelegate 行。我猜这与 Swift 调用 safety 的内容有关...可能是对显式展开的可选值做错了什么?

有什么想法吗?

import Foundation
import UIKit

class GenericCustomPopupViewController: UIViewController {
    @IBOutlet weak var containerView: UIView!
    @IBOutlet weak var popupTitleLabel: UILabel!
    @IBOutlet weak var popupBodyLabel: UILabel!
    @IBOutlet weak var okayButton: UIButton!

   // var okayButtonAction: (()->Void)!

    override func viewDidLoad() {

        okayButton.layer.cornerRadius = 17
        okayButton.layer.borderWidth = 1
        okayButton.layer.borderColor = UIColor(red: 195/255, green: 33/255, blue: 121/255, alpha: 1).CGColor

    }

    func showPopupInView(rootView:UIView) {

       // self.okayButtonAction = completion
        self.view.frame = rootView.bounds
        rootView.addSubview(self.view)

        self.okayButton.addTarget(self, action:"buttonTapped:", forControlEvents:.TouchUpInside)

        self.containerView.center.y = -400
        self.containerView.alpha = 0

        UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 2, options: UIViewAnimationOptions.CurveEaseInOut, animations: ({
        self.containerView.center.y = rootView.center.y
        self.containerView.alpha = 1
    }), completion: nil)

}

    func hidePopupView() {
        self.view.removeFromSuperview()
    }

    func buttonTapped(sender: UIButton!) {
       println("test")
    }


}

这个UIViewController在父视图中是这样实例化的:

var permissionsPopup = GenericCustomPopupViewController(nibName:"LocationPermissionsInfoView", bundle: nil)
    permissionsPopup.showPopupInView(self.view)

啊,找到了

GenericCustomPopupViewController 的 permissionsPopup 引用在 .showPopupInView() 之后被释放。我通过强烈引用它并在我不再需要它时释放它来解决它。