addSubview适用于设备,在模拟器上崩溃

时间:2014-04-13 11:27:59

标签: objective-c ios-simulator

  • 开发环境:XCode 5.1
    • 模拟器硬件:iPhone Retina 4英寸
    • 设备硬件:iPhone 5 16Gb

我在Objective-C中有这个代码可以在设备上运行,但它不在模拟器上发送EXC_BAD_ACCESS:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        CGSize size = frame.size;

        [self setBackgroundColor:[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:1]];
        int radius = size.height * 0.3;

        if (radius < 200)
            radius = 200;

        CGRect rect = CGRectMake(size.width * 0.5 - radius * 0.5, size.height * 0.5 - radius * 0.5, radius, radius);

        projectImage = [[UIMaskedImage alloc] initWithImage:[ProjectController getImageForProject:nil] inFrame:rect withTintColor:[UIColor clearColor] anInsetRadius:6];


        int icon_size = 50;
        int icon_margin = 10;

        menuButton = [[UISVGButton alloc] initWithFrame:CGRectMake(icon_margin, icon_margin, icon_size, icon_size) svgAtPath:@"list.svg" andColor:[UIColor whiteColor]];
        [menuButton addTarget:self action:@selector(closeScene:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:menuButton];

        backButton = [[UISVGButton alloc] initWithFrame:CGRectMake(size.width - icon_size - icon_margin, icon_margin, icon_size, icon_size) svgAtPath:@"arrow_down.svg" andColor:[UIColor whiteColor]];

        [backButton addTarget:self action:@selector(closeOptionsMenu:) forControlEvents:UIControlEventTouchUpInside];


        int icon2_size = 100;


        shareButton = [[UISVGButton alloc] initWithFrame:CGRectMake(size.width*0.5 + radius * 0.5 + icon_margin*2,  size.height*0.5 - icon2_size*0.5, icon2_size, icon2_size)  svgAtPath:@"share.svg" andColor:[UIColor whiteColor]];

        [shareButton addTarget:self action:@selector(shareOnSocialNetworks:) forControlEvents:UIControlEventTouchUpInside];


        deleteSaveButton = [[UISVGButton alloc] initWithFrame:CGRectMake(size.width*0.5 - radius * 0.5 - icon_margin*2 -icon2_size, size.height*0.5 - icon2_size*0.5, icon2_size, icon2_size)svgAtPath:@"delete.svg" andColor:[UIColor whiteColor]];

        [deleteSaveButton addTarget:self action:@selector(downloadCurrentProject:) forControlEvents:UIControlEventTouchUpInside];


        [self setClipsToBounds:YES];

        [self addSubview:backButton];
        [self addSubview:deleteSaveButton];
        [self addSubview:projectImage];
        [self addSubview:shareButton];
    }
    return self;
}

此行崩溃:

[self addSubview:backButton];

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

对于动态(以编程方式)分配的UI元素,始终在拥有它们的视图控制器中保留强引用。

相关问题