2013-03-17 20 views
0

我在空閒時間工作在ipad上,我碰到過一個小問題。在我uisplitview,如果我點擊一個細胞,它加載的圖片,我有另外兩個單元,每個單元有此代碼爲點擊從supreview中刪除,只有它存在

[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)]; 
    [email protected]" 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)]; 
    [email protected]"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];   
} 
+0

你確定它是子視圖的問題,還是它的根'self'被釋放? – CodaFi 2013-03-17 05:20:57

+0

一,這甚至與Xcode沒有遠程相關。二,展示你的代碼。更有可能是你有內存管理錯誤,你的懷疑是不正確的。 – 2013-03-17 05:21:02

+1

這更像是內存問題。你有一個對一個釋放對象的引用。嘗試啓用NSZombie,並參見 – 2013-03-17 05:27:14

回答

0

我的猜測是,你已經在某個時候刪除從它的父self.icon1icon1屬性是weak參考。

這意味着icon1只保留其超級視圖。因此,第一次將其刪除後,它會被釋放,當您嘗試第二次使用它時,會出現錯誤。

解決方案:將icon1屬性更改爲strong引用或(更好),後[self.icon1 removeFromSuperview];

順便說一句,這是Clang靜態分析儀(運行它與Product > Analyze)通常能夠檢測到的那種錯誤。

+0

好吧,去試試。謝謝 – kemnet 2013-03-17 10:58:45

+0

謝謝,設置它們爲零不會給我的錯誤 – kemnet 2013-03-17 21:25:55

+0

你會告訴我爲什麼設置它爲零的作品嗎?它是不會被釋放的? – kemnet 2013-03-18 02:27:29