【问题标题】:addSubview works on device, crashes on SimulatoraddSubview 在设备上工作,在模拟器上崩溃
【发布时间】:2014-04-13 11:27:59
【问题描述】:
  • 开发环境: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];

有什么建议吗?

【问题讨论】:

  • backButton 是强参考吗?您可能在这里遇到了内存管理问题。
  • [self.view addSubview:backButton];
  • @Rushi,这个类本身就是一个 UIView。我无法使用 [self.view addSubView:backButton] 添加视图。只有 [self addSubview: backButton]
  • @HermannKlecker 我尝试将所有子视图声明为 __weak,但仍然无法正常工作。此时内存仅为 16MB,CPU 为 2%。
  • 在调试器中,你检查了 backButton(和 self)吗?你试过 Instruments 吗?

标签: objective-c ios-simulator


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-06
    相关资源
    最近更新 更多