仅在存在时才从supreview中删除

时间:2013-03-17 05:13:14

标签: ios objective-c cocoa-touch

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

[self.icon1 removeFromSuperview];

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

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

1 个答案:

答案 0 :(得分:0)

我的猜测是,您已经在某个时刻从其超级视图中删除了self.icon1,并且icon1属性是weak引用。

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

解决方案:将icon1属性更改为strong引用或(更好),self.icon1 = nil之后执行[self.icon1 removeFromSuperview];

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