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

时间:2015-01-28 13:06:05

标签: ios iphone swift

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

特别是它会显示class AppDelegate: UIResponder, UIApplicationDelegate行,错误为Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)。我猜这是关于什么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)

1 个答案:

答案 0 :(得分:1)

啊,发现了。

GenericCustomPopupViewController之后,.showPopupInView()的permissionsPopup引用被取消分配。我通过强烈引用它来解决它,并在我不再需要它时解除分配。