有时 EXC_BAD_ACCESS 有时无法识别的选择器发送到实例

Sometimes EXC_BAD_ACCESS and sometimes unrecognized selector sent to instance

我有这个问题,我查看了很多主题但没有找到答案,我正在使用 Swift。

我正在向 url 发送一个异步请求,它 returns 给我一个 xml 代码,我在 NSMutableArray 的自定义对象中解析和排列它的内容和往常一样。

假设数组包含 class AppsObject 的 3 个对象,其中包含 ID、图像的 URL 字符串、文本和 URL 字符串,以便稍后用于 UIButton 转到由浏览器指定 url 页面。

我制作了一个名为 AppsView 的 class,它有一个裸初始化程序和另一个自定义视图位置和大小的初始化程序,并为上面提到的 link 添加了一个 ImageIcon 和一个按钮.

在主视图控制器中,我在 for 循环中启动 appview 以创建多个包含 NSmutableArray 数据的 appview。

我收到错误消息是因为动态生成的 UIButton 并将 (addTarget) 设置为同一对象中的函数。

有人可以帮助我吗? 您将找到以下代码:

class AppView: NSObject {

    var BackView = UIView()
    var AppIconView = UIImageView()
    var DescriptionLabel = UILabel()
    var LinkIcon = UIImageView()
    var AppNo:Int!
    var DescriptionText:String!

    let originView:UIView!
    let LinkImage = UIImage(named: "HyperLink.png")
    var LinkButton = UIButton.buttonWithType(UIButtonType.System) as UIButton

    let BackViewHeight:CGFloat = 130.0
    let AppIconSize:CGFloat = 110.0
    let Inset:CGFloat = 5.0
    let BackgroundAlpha:CGFloat = 0.5
    let LinkButtonSize:CGFloat = 35.0
    let LinkButtonURL = NSURL()



    override init() {
        super.init  ()
    }

    init(sourceView:UIView, AppIconURLString: String , LoadedDescription: String , ObjectNo: Int , LinkButtonURL: NSURL){
        super.init()

        originView = sourceView
        ImagesLoader.downloadImage(NSURL(string: AppIconURLString)!, handler: {image, error in
            self.AppIconView.image = image
        })

        DescriptionText = LoadedDescription
        AppNo = ObjectNo

        setupAppView()
    }


    func setupAppView() {
        //SetupBackView
        BackView.frame = CGRectMake(Inset,
                                    Inset + (CGFloat(AppNo) * 140.0),
                                    originView.bounds.size.width - (Inset * 2),
                                    BackViewHeight)

        BackView.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(BackgroundAlpha)

        BackView.clipsToBounds = false
        originView.addSubview(BackView)

        //SetupAppIconView
        AppIconView.frame = CGRectMake(BackView.bounds.size.width - AppIconSize - Inset*2, Inset*2, AppIconSize, AppIconSize)

        AppIconView.clipsToBounds = false
        BackView.addSubview(AppIconView)

        //SetupLabelView
        DescriptionLabel.frame = CGRectMake(Inset, Inset, BackView.bounds.size.width - Inset - AppIconSize - Inset, AppIconSize)
        DescriptionLabel.text = DescriptionText
        DescriptionLabel.textColor = UIColor.whiteColor()

        BackView.addSubview(DescriptionLabel)

        //SetupButtonView
        LinkButton.frame = CGRectMake(Inset, BackView.bounds.size.height - LinkButtonSize - Inset, LinkButtonSize, LinkButtonSize)
        LinkButton.setImage(LinkImage, forState: .Normal)
        LinkButton.addTarget(self, action: "GotoLinkURL:", forControlEvents:UIControlEvents.TouchUpInside)
        LinkButton.tag = AppNo
        BackView.addSubview(LinkButton)
    }

    func GotoLinkURL(sender:UIButton!) {
        println(sender.tag)
    }

}

我发现我的问题出在哪里了。我阅读了一些 Apple 内存管理 并阅读了有关 NSZombie 的内容,这让我知道 NSObject 已从内存中删除,因为多种原因,它们是:

1) 我没有在调用 class 的开头声明它,即 MainViewController 所以当它完成所有启动过程时,它会关闭并从内存中删除等等 GotoLinkURL 函数不存在。

2) 然后我在 MainViewController 中声明但我发现最后一个对象只存在于内存中因为我在 for 循环语句中调用它所以我持有所有 NSObjects在一个数组中,这就成功了。