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

标签 ios iphone swift

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

特别是它显示了 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 在父 View 中是这样实例化的:

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

最佳答案

啊,找到了。

GenericCustomPopupViewController 的 permissionsPopup 引用在 .showPopupInView() 之后被释放。我通过对它进行强引用并在以后不再需要它时取消分配来解决它。

关于ios - 当 UIButton 调用其选择器操作时获取 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28193077/

相关文章:

iphone - 如何访问附加到 UIGestureRecognizer 的选择器的名称?

ios - 选项卡栏 Controller 问题内的 UI 导航 Controller

ios - 不断调用方法,直到 View 被取消

iPhone - 将文件从资源复制到文档会出错

ios - Swift - 如何识别单击和双击之间的区别

ios - 使用 SkpsmtpMessage 库格式化电子邮件正文

iphone - 如何访问WiFiManager Framework iOS?

iphone - IOS5无法使用核心数据库

swift - 调用请求时字符串集被清除 : Swift in-app purchases

iOS swift : Send log information to iOS app from iOS framework