Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.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
iphone-Objective-C“;发送到解除分配实例的消息_Objective C_Xcode - Fatal编程技术网

iphone-Objective-C“;发送到解除分配实例的消息

iphone-Objective-C“;发送到解除分配实例的消息,objective-c,xcode,Objective C,Xcode,我尝试使用此方法获取所有子视图,但当我运行应用程序时,有一条消息发送到deallocated instance error subviewsArray = self.view.subviews; for (int i=0; i <subviewsArray.count; i++) { UIView *subview = [subviewsArray objectAtIndex:i]; if (subview.tag >= 0) { [subview r

我尝试使用此方法获取所有子视图,但当我运行应用程序时,有一条消息发送到deallocated instance error

subviewsArray = self.view.subviews;
for (int i=0; i <subviewsArray.count; i++) {
    UIView *subview = [subviewsArray objectAtIndex:i];
    if (subview.tag >= 0) {
        [subview removeFromSuperview];
    }
}
试试这个:

for(UIView *temp in self.view.subviews )
{
    [temp removeFromSuperview];
}
或者这个:

[self.view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
希望这有帮助

干杯

试试这个:

for(UIView *temp in self.view.subviews )
{
    [temp removeFromSuperview];
}
 self.subviewsArray = self.view.subviews;
for (int i=0; i <self.subviewsArray.count - 1; i++) {
    UIView *subview = [self.subviewsArray objectAtIndex:i];
    if (subview.tag >= 0) {
        [subview removeFromSuperview];
    }
}
或者这个:

[self.view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
希望这有帮助

干杯

self.subviewsArray=self.view.subviews;
 self.subviewsArray = self.view.subviews;
for (int i=0; i <self.subviewsArray.count - 1; i++) {
    UIView *subview = [self.subviewsArray objectAtIndex:i];
    if (subview.tag >= 0) {
        [subview removeFromSuperview];
    }
}
对于(int i=0;i=0){ [子视图从SuperView移除]; } }
self.subview sarray=self.view.subview;
对于(int i=0;i=0){
[子视图从SuperView移除];
}
}

首先,虽然可能是ARC造成的,但简化for循环可能会解决这个问题

for (UIView *subview in self.subviews)
{

}   

首先,虽然可能是ARC造成的,但简化for循环可能会解决这个问题

for (UIView *subview in self.subviews)
{

}   

将循环代码更改为如下所示:

subviewsArray = self.view.subviews;
for (int i=0; i <subviewsArray.count - 1; i++) {
    UIView *subview = [subviewsArray objectAtIndex:i];
    if (subview.tag >= 0) {
        [subview removeFromSuperview];
    }
}
subviewsArray=self.view.subviews;
对于(int i=0;i=0){
[子视图从SuperView移除];
}
}

将循环代码更改为如下所示:

subviewsArray = self.view.subviews;
for (int i=0; i <subviewsArray.count - 1; i++) {
    UIView *subview = [subviewsArray objectAtIndex:i];
    if (subview.tag >= 0) {
        [subview removeFromSuperview];
    }
}
subviewsArray=self.view.subviews;
对于(int i=0;i=0){
[子视图从SuperView移除];
}
}

将循环代码更改为:

for (int i=subviewsArray.count - 1; i >= 0; --i) {
    UIView *subview = [subviewsArray objectAtIndex:i];
    if (subview.tag >= 0) {
        [subview removeFromSuperview];
    }
}
出现此问题的原因是,当循环从0前进到第10个(假设为最后一个)视图时,您开始从父视图中删除它们,因此计数也会发生变化,剩余视图的索引也会发生变化

最后,您尝试删除一个视图并向其发送一条消息removeFromSuperview,该消息已被解除分配,这一切都是由于索引中的更改。 最好倒过来,从最后一个索引到第一个索引

我很惊讶,为什么你们并没有得到索引越界异常


不仅仅是在这里,一般来说,每当您遍历基于索引的集合并尝试删除元素时,都会遇到此问题。

将循环代码更改为:

for (int i=subviewsArray.count - 1; i >= 0; --i) {
    UIView *subview = [subviewsArray objectAtIndex:i];
    if (subview.tag >= 0) {
        [subview removeFromSuperview];
    }
}
出现此问题的原因是,当循环从0前进到第10个(假设为最后一个)视图时,您开始从父视图中删除它们,因此计数也会发生变化,剩余视图的索引也会发生变化

最后,您尝试删除一个视图并向其发送一条消息removeFromSuperview,该消息已被解除分配,这一切都是由于索引中的更改。 最好倒过来,从最后一个索引到第一个索引

我很惊讶,为什么你们并没有得到索引越界异常



不仅仅是在这里,一般来说,每当你遍历一个基于索引的集合并尝试删除元素时,你总是会遇到这个问题。

@Pfitz你听说过属性吗?@guitarflow NSArray没有属性计数…@Pfitz,对不起,我想点运算符只用于属性。我的错。@JamesWebster ok ok它也是这样工作的-仍然不喜欢:)@MrA59这段代码中的一切看起来都很好,可能是您正在释放dealloc方法中的subviewsArray,它没有被保留。@Pfitz您听说过属性吗?@guitarflow NSArray没有属性计数…@Pfitz,对不起,我以为点运算符只用于属性。我的错。@JamesWebster ok ok它也是这样工作的-仍然不喜欢:)@MrA59这段代码中的一切看起来都很好,可能是您正在释放dealloc方法中的subviewsArray,它没有被保留。我尝试了您的方法,但我仍然有问题。[MyViewControllerExample view]:消息发送到解除分配的实例0x7268870,那个么您最好的机会是使用僵尸工具。确保您在模拟器上运行应用程序,因为此仪器在设备上不可用。走上坠机前的台阶,看看仪器在告诉你什么。也许有了更多的信息,我们可以找到一个解决方案。我不能使用模拟器,因为我需要连接一个传感器来使用iphone进行测试!谢谢你的帮助!然后将代码放在@try catch语句中,并打印出异常描述。此外,这可能有点过于硬核,但我发现有时打印出内存地址并查看哪个对象给我带来了困难很有用。我尝试了以下方法:@try{subviewsArray=self.view.subviews;}@catch(NSException*e){NSLog(@“Exception:%@”,e);}错误在subviewsArray行中!它在这一行阻塞并且不显示异常!我尝试了你的方法,但我仍然有问题。[MyViewControllerExample view]:消息发送到解除分配的实例0x7268870,那个么您最好的机会是使用僵尸工具。确保您在模拟器上运行应用程序,因为此仪器在设备上不可用。走上坠机前的台阶,看看仪器在告诉你什么。也许有了更多的信息,我们可以找到一个解决方案。我不能使用模拟器,因为我需要连接一个传感器来使用iphone进行测试!谢谢你的帮助!然后将代码放在@try catch语句中,并打印出异常描述。此外,这可能有点过于硬核,但我发现有时打印出内存地址并查看哪个对象给我带来了困难很有用。我尝试了以下方法:@try{subviewsArray=self.view.subviews;}@catch(NSException*e){NSLog(@“Exception:%@”,e);}错误在subviewsArray行中!它在这一行阻塞并且不显示异常<代码>