0

我在业余时间正在开发一个 ipad 程序,我遇到了一个小问题。在我的 uisplitview 中,如果我单击一个单元格,它会加载一张图片我有两个其他单元格,每个单元格都有这个代码

[self.icon1 removeFromSuperview];

但是,这可以很好地删除图片。如果在我单击带有图片的单元格一之后...然后我单击单元格 2(并且运行此删除代码)它会正确删除图片。但如果我单击单元格 3...然后程序中断(访问不正确,它指向 removeFromSuperview)。虽然我认为这是因为图标已被删除。是否有一个功能基本上说“如果存在则从超级视图中删除”?谢谢你

if (([receivedRainObject isEqualToString:@"Facts"]) && (Track==100)) {
    self.view.backgroundColor=[UIColor whiteColor];
    //     self.tvFacts=[[UITextView alloc]initWithFrame:CGRectMake(0, 150, 700, 500)];
    self.tvFacts.text=@"  Test";

    [tvFacts setEditable:NO];

     UIImage *acheivement1=[UIImage imageNamed:@"saychesse.png"];
    [factBanner1 setImage:acheivement1];

    UIImage *fbIcon= [UIImage imageNamed:@"facebook.png"];
     fbIcon1 = [UIButton buttonWithType:UIButtonTypeCustom];
    fbIcon1.frame = CGRectMake(580.0, 305.0, 35.0, 35.0);
    fbIcon1.tag=TAG_BUTTON_ONE;
    [fbIcon1 addTarget:self action:@selector(authButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [fbIcon1 setBackgroundImage:fbIcon forState:UIControlStateNormal];

    UIImage *twIcon= [UIImage imageNamed:@"twitter.png"];
    twIcon1 = [UIButton buttonWithType:UIButtonTypeCustom];
    twIcon1.frame = CGRectMake(530.0, 305.0, 35.0, 35.0);
    [twIcon1 addTarget:self action:@selector(tweetTapped) forControlEvents:UIControlEventTouchUpInside];
    [twIcon1 setBackgroundImage:twIcon forState:UIControlStateNormal];

    [self.imgClassification removeFromSuperview];
    [self.Image removeFromSuperview];

    [self.lblSel removeFromSuperview];
    [self.tvDescrip removeFromSuperview];
    [self.tvName removeFromSuperview];
    [self.tvDiet removeFromSuperview];
    [self.navigationController popViewControllerAnimated:NO];
    [self.lblPictureAnnotation removeFromSuperview];
    [self.lbllife removeFromSuperview];
    [self.tvGestation removeFromSuperview];

    [self.view addSubview:self.tvFacts];
    [self.view addSubview:factBanner1];
    [self.view addSubview:fbIcon1];
    [self.view addSubview:twIcon1];
    [self.view setNeedsDisplay];

}
// Life-Span         
if (([receivedRainObject isEqualToString:@"Life Span"]) && (Track==100)) {
    self.view.backgroundColor=[UIColor whiteColor];
    //     self.lbllife=[[UILabel alloc]initWithFrame:CGRectMake(0, 150, 700, 500)];
    self.lbllife.text=@"The";

    [lbllife setEditable:NO];
    [self.factBanner1 removeFromSuperview];
   [self.fbIcon1 removeFromSuperview];
    [self.twIcon1 removeFromSuperview];
     [self.factBanner2 removeFromSuperview];
      [self.fbIcon2 removeFromSuperview];
    [self.twIcon2 removeFromSuperview];
    [self.imgClassification removeFromSuperview];
    [self.Image removeFromSuperview];

    [self.lblSel removeFromSuperview];
    [self.tvDescrip removeFromSuperview];
    [self.tvName removeFromSuperview];
    [self.tvDiet removeFromSuperview];
    [self.navigationController popViewControllerAnimated:NO];
    [self.tvFacts removeFromSuperview];
    [self.lblPictureAnnotation removeFromSuperview];
    [self.tvGestation removeFromSuperview];
     [self.factBanner1 removeFromSuperview];
    [self.view addSubview:self.lbllife];
    [self.view setNeedsDisplay];        
}
4

1 回答 1

0

我的猜测是你已经self.icon1在某个时候从它的超级视图中删除了,并且该icon1属性是一个weak参考。

这意味着icon1仅由其超级视图保留。因此,在您第一次删除它后,它会被释放,当您再次尝试使用它时,您会收到错误消息。

解决方案:要么将icon1属性更改为strong引用,要么(更好),self.icon1 = nil[self.icon1 removeFromSuperview];.

Product > Analyze顺便说一句,这是 Clang 静态分析器(运行它)通常能够检测到的那种错误。

于 2013-03-17T09:43:44.487 回答