objective-c - UIButton 使用 ARC 触及导致 EXC_BAD_ACCESS 的 IBAction

标签 objective-c ios debugging exc-bad-access

StackOverflow 上有几个问题,用户遇到了与我遇到的相同问题。但是,他们的解决方案都不适合我的情况。 (请参阅 herehereherehere 了解一些我已阅读但未找到帮助的 SO 问题。)

在我的例子中,我有一个 NIB,它有几个 UIButton,以及一个关联的 Controller View 。该 View 对我的项目来说相对较旧,直到今天我才能够毫无问题地使用这些按钮。在进行了一些与按钮行为无关的代码更改后,我遇到了一个使应用程序崩溃的错误,破坏了 main() 函数中的代码并给了我一个 每当我触摸我 View 上的任何按钮时,都会出现 EXC_BAD_ACCESS 错误消息。

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

我的项目正在使用自动引用计数,我以前从未见过这个错误。此外,我没有修改 NIB,也没有修改与按钮关联的 IBAction,所以我看不出是什么原因导致的。停止错误的唯一方法是取消链接我的 NIB 中的 UIButton 到我的 Controller View 头文件中定义的 IBAction 方法。

我的用例的唯一“独特”方面是我在另一个 subview Controller 中加载该 View 的一个或两个实例。加载的断开 View 的实例数取决于数组中的对象数。下面是我用来实例化这些 View 并将其加载为另一个 View 的 subview 的代码。

//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;
}

这工作得很好,以前,甚至没有对关联 View 上的按钮造成任何问题,但是,现在所有 UIButton 在点击时都会使应用程序崩溃。

SubViewController 的初始化函数以及 viewDidLoad 方法仅包含创建新 ViewController 时添加的标准自动生成代码。

我该怎么做才能修复或诊断此问题?

最佳答案

在你的代码中查看我的注释:

{
    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
}

要解决此问题,请将每个 SubViewController 实例添加到主视图 Controller 拥有的数组中。这将使 SubViewController 实例保持接收按钮点击。

关于objective-c - UIButton 使用 ARC 触及导致 EXC_BAD_ACCESS 的 IBAction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9103705/

相关文章:

ios - iOS 视网膜显示屏上的坐标和尺寸已关闭

objective-c - 标准化顶点和法线坐标 Open GL ES 2.0

ios - 将 -apple-system 用于等宽和衬线

ruby-on-rails - 如何使用 eclipse 调试 jRuby?

php - 当 WP_DEBUG_LOG 设置为 true 时,debug.log 中没有显示调试输出,为什么?

objective-c - 如何在 Cocoa 中自定义 NSTextField 外观(使用的字体、字体大小)?

android - 有没有办法让移动应用程序向用户付费?

ios - 无法使用助理编辑器将表格单元 UI 连接到代码

visual-studio-2010 - 调试外部程序的库? (Visual Studio 2010)

objective-c - 如何知道谁发送了消息