Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c UIButton触动iAction,导致EXC\u坏\u与ARC的访问_Objective C_Ios_Debugging_Exc Bad Access - Fatal编程技术网

Objective c UIButton触动iAction,导致EXC\u坏\u与ARC的访问

Objective c UIButton触动iAction,导致EXC\u坏\u与ARC的访问,objective-c,ios,debugging,exc-bad-access,Objective C,Ios,Debugging,Exc Bad Access,有一些关于StackOverflow的问题,用户遇到了与我相同的问题。然而,他们没有一个解决方案适合我的情况。(请参阅,对于一些我已经阅读但没有发现有帮助的问题,请参阅和。) 在我的例子中,我有一个NIB,它有一对ui按钮s,以及一个关联的控制器视图。这个视图对我的项目来说比较旧,直到今天,我才能够毫无困难地使用这些按钮。在做了一些与按钮行为无关的代码更改后,我遇到了一个错误,导致应用程序崩溃,在main()函数中中断了代码,每当我触摸视图中的任何按钮时,都会显示EXC\u BAD\u ACCE

有一些关于StackOverflow的问题,用户遇到了与我相同的问题。然而,他们没有一个解决方案适合我的情况。(请参阅,对于一些我已经阅读但没有发现有帮助的问题,请参阅和。)

在我的例子中,我有一个NIB,它有一对
ui按钮
s,以及一个关联的控制器视图。这个视图对我的项目来说比较旧,直到今天,我才能够毫无困难地使用这些按钮。在做了一些与按钮行为无关的代码更改后,我遇到了一个错误,导致应用程序崩溃,在
main()
函数中中断了代码,每当我触摸视图中的任何按钮时,都会显示
EXC\u BAD\u ACCESS
错误消息

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

我的项目正在使用自动引用计数,我以前从未见过这个错误。此外,我没有修改NIB,也没有修改与按钮关联的
iAction
,因此我看不出是什么导致了这种情况。停止错误的唯一方法是取消NIB中my
ui按钮
s与控制器视图头文件中定义的
iAction
方法的链接

我的用例唯一“独特”的方面是在另一个子视图控制器中加载该视图的一个或两个实例。加载的断开视图实例的数量取决于阵列中对象的数量。下面是我用来实例化这些视图并将其作为另一个视图的子视图加载的代码

//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
s在点击时都会使应用程序崩溃

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
}

要解决此问题,请将每个子ViewController实例添加到主视图控制器拥有的数组中。这将保留SubViewController实例以接收按钮点击。

请确保在您的dealloc中调用:

[按钮移除目标:无 操作:空 forControlEvents:uicontrol事件事件事件]


尽管您不需要ARC中的“dealloc”,但您确实需要,因为iccir解释了这一点。

有趣的是,我到了办公桌前会尝试一下。我明白为什么这会解决问题,但我有点困惑——我已经有了这个机制好几个星期了。我没有遇到任何问题,也没有对相关代码进行过调整和修改。如果你所说的是问题所在,那么这似乎永远都是个问题,而不仅仅是在某一天。你知道为什么在某些情况下这会起作用吗?你最近升级到ARC了吗?这看起来像是手动引用计数中的漏洞。也有可能您之前有其他东西保留子ViewController并使其保持活动状态-如果您使用计时器,则可能是运行循环,如果您正在制作动画,则可能是UIViewAnimation.iccir-很抱歉响应太晚。我刚注意到你的最后一个问题。这是我在12月初开始的一个新项目。由于我是苹果发展的新手,我所经历的只是ARC开发。这很方便,但我觉得我错过了很多理解,因为我从来没有在objective-c的传统模式下编程。你是天才!您保存了我的一天。如果所有这些UIView对象都是在IB中创建并通过IBOutlet连接的,是否有必要这样做?我从来没有读过这样做的指令。是的,尤其是IB。IB创建,但它不处理对象的销毁。