2016-03-08 103 views
1

我的應用程序在某個地方崩潰,我無法找到它發生的原因,請查看我的代碼,並告訴我如果我做錯了什麼。它似乎在UIScrollView崩潰,我有一個UIButton其中我打電話給這種方法。EXC_BAD_ACCESS KERN_INVALID_ADDRESS在uiscrollview中崩潰

- (void) designForTestFilereIPad:(NSArray *)bdfValue filer:(NSString *)filer 
{ UIView *legendView = [[UIView alloc] init]; 

UIScrollView *legendScrollView=[[UIScrollView alloc] init]; 
UILabel *label; 

if ([[User currentUser].UserProfile.crmWebHomeUrl length]) 
{ 
    MenuItemView *crmMenuItemView = [self->_menuItemViews objectAtIndex:[self->_menuItemViews count] - 3]; 

    legendView.frame = CGRectMake(0, crmMenuItemView.frame.origin.y, self.frame.size.width - 1, 100); 

} 
else 
{ 
    MenuItemView *supportMenuItemView = [self->_menuItemViews objectAtIndex:[self->_menuItemViews count] - 2]; 

    legendView.frame = CGRectMake(0, supportMenuItemView.frame.origin.y - 150, self.frame.size.width - 1, 150); 

    //To do:Legend scroll view 
    legendScrollView.frame = CGRectMake(0,20, self.frame.size.width - 1, legendView.frame.size.height-20); 
    [legendScrollView setShowsVerticalScrollIndicator:YES]; 
    legendScrollView.indicatorStyle=UIScrollViewIndicatorStyleWhite; 
    [legendScrollView setScrollEnabled:YES]; 
    [legendScrollView setPagingEnabled:NO]; 
} 

[legendView setTag:101]; 

CGPoint start = CGPointMake(SUPERVIEW_PAD_HORIZONTAL, PAD_VERTICAL); 

CGRect lblLegendFrame = CGRectMake(15 ,PAD_VERTICAL_LEGEND, 100, 14); 

UILabel *lblLegend = [[[UILabel alloc] initWithFrame:lblLegendFrame] autorelease]; 

lblLegend.text = filer; 
[lblLegend setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]]; 
[lblLegend setTextColor:[UIColor colorWithRed:0.59 green:0.59 blue:0.59 alpha:1.0]]; 

[legendView addSubview:lblLegend]; 

CGRect topSepratorFrame = CGRectMake(0, lblLegendFrame.size.height+5, legendView.frame.size.width, 1); 
UILabel *separatorLine = [[[UILabel alloc] initWithFrame:topSepratorFrame] autorelease]; 
[separatorLine setBackgroundColor:[UIColor colorWithRed:0.27 green:0.25 blue:0.25 alpha:1.0]]; 
[legendView addSubview:separatorLine]; 

NSArray *arrColors = [self getColors:[bdfValue count]]; 

for (int i = 0; i < [bdfValue count]; i++) 
{ 
    // Making frames for UI (Image, Label) 

    CGRect imgFrame; 
    imgFrame= CGRectMake(start.x, start.y, 12, 12); 

    CGSize maximumLabelsize = CGSizeMake(CGFLOAT_MAX, imgFrame.size.height); 

    CGSize expectedLabelSize = [[bdfValue objectAtIndex:i] boundingRectWithSize:maximumLabelsize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:11]} context:nil].size; 

    //  CGRect lblFrmae = CGRectMake(imgFrame.origin.x + imgFrame.size.width + PADDING, imgFrame.origin.y, expectedLabelSize.width, LABEL_HEIGHT); 
    CGRect lblFrmae = CGRectMake(start.x + 25 ,start.y, expectedLabelSize.width, 12); 


    if (lblFrmae.origin.x + lblFrmae.size.width + SUPERVIEW_PAD_HORIZONTAL > 156) // check if label exceeds the frame width., make next row 
    { 
     start.x = SUPERVIEW_PAD_HORIZONTAL; 
     if(i!=0) 
     { 
     float point=expectedLabelSize.width/121; 
     int padding=point*12; 
     start.y = start.y +padding; 
     } 
     imgFrame = CGRectMake(start.x, start.y, 12, LABEL_HEIGHT_CUST); 

     lblFrmae = CGRectMake(imgFrame.origin.x + imgFrame.size.width + 8, imgFrame.origin.y, 121, LABEL_HEIGHT_CUST); 

     if (start.y > self.frame.size.height) // check if label exceeds the frame height 
     { 
      CGRect viewFrame = self.frame; 
      viewFrame.size.height = viewFrame.size.height + LABEL_HEIGHT + PAD_VERTICAL; 
      viewFrame.origin.y = viewFrame.origin.y - (LABEL_HEIGHT + PAD_VERTICAL); 
     } 
    } 

    // Giving frames to its UI 

    UIImageView *image = [[[UIImageView alloc] initWithFrame:imgFrame] autorelease]; 
    image.layer.cornerRadius = 6.0f; 
    image.layer.masksToBounds = YES; 
    if ([arrColors count] > i) 
     [image setBackgroundColor:[arrColors objectAtIndex:i]]; 
    else 
     [image setBackgroundColor:nil]; 

    label = [[[UILabel alloc] initWithFrame:lblFrmae] autorelease]; 
    label.text = [bdfValue objectAtIndex:i]; 
    [label setFont:[UIFont systemFontOfSize:11]]; 
    label.numberOfLines=0; 
    [label setTextColor:[UIColor whiteColor]]; 
    [label sizeToFit]; 
    [legendScrollView addSubview:image]; 
    [legendScrollView addSubview:label]; 
}//Crashes at below line. 
[legendScrollView setContentSize:CGSizeMake(self.frame.size.width - 1,start.y+label.frame.size.height - 5)]; 
[legendView addSubview:legendScrollView]; 
[self addSubview:legendView]; 
[legendScrollView release]; 
[legendView release];} 
+1

哪條線路導致崩潰? – RaffAl

+0

您是否在Interface Builder中刪除了相同的東西? –

+0

@Bearwithme此行崩潰'[legendScrollView setContentSize:CGSizeMake(self.frame.size.width - 1,start.y + label.frame.size.height - 5)];' –

回答

0

通常會發生這種情況,因爲您正在使用的對象已被釋放。

[legendScrollView setContentSize:CGSizeMake(self.frame.size.width - 1,start.y+label.frame.size.height - 5)]; 

,因爲下面的代碼可能是你正在使用setContentSize被釋放標籤:

label = [[[UILabel alloc] initWithFrame:lblFrmae] autorelease]; 

入住的legendScrollViewsetContentSize通過靜態值之後。

autoreleasesetContentSizelegendScrollView之後的標籤上。

+0

所以這意味着我們應該在啓動時自動分配init標籤,而不是在開始時將它作爲'UILabel *標籤;'寫入。對? –

+0

在'setContentSize'之後做autorelease @ uzair dhada –

+0

您在使用標籤高度的'setContentSize'中傳遞了靜態值後是否檢查過?@ uzairdhada –

相關問題