UIButton触摸到使用ARC导致EXC_BAD_ACCESS的IBAction [英] UIButton touches up IBAction causing EXC_BAD_ACCESS with ARC

查看:147
本文介绍了UIButton触摸到使用ARC导致EXC_BAD_ACCESS的IBAction的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在StackOverflow中有一些问题,用户遇到与我相同的问题。但是,他们的解决方案都不符合我的情况。 (请参见此处 here here 这里对于我读过的一些SO问题,但没有找到帮助。)

There have been a few questions on StackOverflow where users have experienced the same problem as I am having. However, none of their solutions fit my case. (See here, here, here and here for some of the SO questions I've read but have not found helpful.)

在我的情况下,我有一个NIB有一对夫妇 UIButton ,其中包含一个关联的控制器视图。该视图对我的项目来说比较老,直到今天,我已经能够使用这些按钮没有任何麻烦。在进行与按钮行为无关的几个代码更改后,我遇到了一个错误,导致应用程序崩溃,并将代码分解到 main()函数和当我触摸我的视图中的任何按钮时,会给我一个 EXC_BAD_ACCESS 错误消息。

In my case, I have a NIB that has a couple UIButtons, with an associated Controller View. The view is relatively old to my project and I've been able to use these buttons without any trouble until today. After making a few code changes that were not related to the button behavior, I've run into an error that crashes the app, breaks the code at the main() function and gives me an EXC_BAD_ACCESS error message whenever I touch any of the buttons on my View.

如何或为什么会发生这种情况?我实际上已经评论过几乎所有的功能代码,特别是我今天早些时候修改的代码,我仍然无法阻止错误发生。

How or why might this happen? I've actually commented out almost all functional code, especially that which I modified earlier today and I still can't stop the error from occurring.

我的项目正在使用自动引用计数,之前我还没有看到这个错误。此外,我没有修改NIB,也没有修改与按钮相关联的 IBAction ,所以我看不到会导致什么。阻止错误的唯一方法是将我的NIB中的 UIButton 取消链接到我的Controller中定义的 IBAction 方法查看头文件。

My project is using Automatic Reference Counting and I haven't seen this error before. Furthermore, I did not modify the NIB, nor the IBAction associated with the buttons so I don't see what would cause this. The only way to stop the error is to unlink my UIButtons in my NIB to the IBActionmethods defined in my Controller View header file.

我的用例的唯一唯一方面是在另一个子视图控制器中加载此视图的一个或两个实例。被加载的断开视图的实例数量取决于数组中的对象数量。以下是我用于实例化和加载这些视图作为另一个视图的子视图的代码。

The only "unique" aspect of my use-case is that I load either one or two instances of this view, within another sub view controller. The number of instances of the broken view that are loaded is contingent on the number of objects in an array. Below is the code that I use to instantiate and load these views as subviews of another view.

//Called else where, this starts the process by creating a view that 
//will load the problematic view as a sub-view either once or twice.
- (id)initWithPrimarySystemView:(SystemViewController *)svc
{
    //First we create our parent, container view.
    self = [super initWithNibName:@"ContainerForViewInstaniatedFromArrayObjs" bundle:nil];
    if (self) 
    {
        //Assign parent DataModel to local instance
        [self setDataModel:((DataModelClass*)svc.DataModel)];
        for (AnotherModel* d in DataModel.ArrayOfAnotherModels)
        {
            //Instantiate the SubViewController.
            SubViewController* subsvc = [[SubViewController alloc] 
                                            initWithNibName:@"Subview" 
                                          bundle:nil 
                                          subviewPosition:d.Position ];

            //Add the SubViewControllers view to this view.
            [subsvc.view setFrame:CGRectMake((d.Position-1)*315, 0, 315, 400)];
            [self.view addSubview:subsvc.view];
        }
        [self setDefaultFrame: CGRectMake(0, 0, 640, 400)];
    }
    return self;
}

这样做完美,以前甚至没有造成任何麻烦然而,在相关视图中的按钮现在全部 UIButton 在点击时崩溃了应用程序。

This works perfectly and, previously, hadn't even caused any trouble with the buttons that were on the associated view, however, now all UIButtons crash the app when tapped.

初始化SubViewController的功能以及 viewDidLoad 方法除了创建新的ViewController之后添加的标准自动生成的代码之外,还没有任何其他的东西。

The initialization function for SubViewController, as well as the viewDidLoad method contain nothing other than the standard, auto-generated code that is added when you create a new ViewController.

我可以做些什么来修复或诊断这个问题?

What can I do to either fix or diagnose this problem?

推荐答案

你的代码:

{
    SubViewController* subsvc = [[SubViewController alloc] initWithNibName:@"Subview" bundle:nil subviewPosition:d.Position ];
    //!i: By default, subsvc is a __strong pointer, so your subview has a +1 retain count
    //    subsvc owns subsvc.view, so subsvc.view has a +1 retain count as well

    //Add the SubViewControllers view to this view.
    [subsvc.view setFrame:CGRectMake((d.Position-1)*315, 0, 315, 400)];

    [self.view addSubview:subsvc.view];
    //!i: This bumps subsvc.view to +2, as self.view strong-references it

    //!i: subsvc is going out of scope, so the reference count on subsvc will drop
    //    to 0 and it is dealloc'd.  subsvc.view's retain count drops to +1, as it
    //    is still referenced by self.view
    //
    //    Most likely, in -[SubViewController dealloc], you were not doing a 
    //    setTarget:nil, setAction:nil on the button.  Thus, the button now 
    //    has a dangling pointer and will crash when hit
}


$ b $时会崩溃b

要解决此问题,请将每个SubViewController实例添加到主视图控制器拥有的数组中。这将保持SubViewController实例的周围接收按钮水龙头。

To fix this, add each SubViewController instance to an array owned by the master view controler. That will keep the SubViewController instances around to receive the button taps.

这篇关于UIButton触摸到使用ARC导致EXC_BAD_ACCESS的IBAction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆