gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:11:46 29 4
gpt4 key购买 nike

我有这段代码,我正在尝试为 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com