uiscrollview中的EXC_BAD_ACCESS KERN_INVALID_ADDRESS崩溃

时间:2016-03-08 09:17:05

标签: ios objective-c

我的应用程序在某个地方崩溃,我无法找到它发生的原因,请查看我的代码,并告诉我,如果我做错了什么。它似乎在UIScrollView发生了崩溃,我有一个UIButton我正在调用这种方法。

- (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 个答案:

答案 0 :(得分:0)

通常会发生这种情况,因为您正在使用的对象已被解除分配。

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

因为下面的代码可能是您在setContentSize中使用的标签已取消分配:

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

setContentSize的{​​{1}}中传递静态值后检查。

legendScrollView setContentSize后的标签上执行自动释放