【问题标题】:Adding a CALayer sublayer inside of UIView init在 UIView init 中添加 CALayer 子层
【发布时间】:2013-04-05 11:26:34
【问题描述】:

我正在尝试将 CALayer 添加为 UIView 子类中的子层,但是当我在 init 方法中添加子层时,当我将视图添加到另一个视图或窗口时,我得到 EXC_BAD_ACCESS

初始化方法:

- (id)initWithTitle:(NSString *)title message:(NSString *)message
{
    if ((self = [super init]))
    {
        self.title = title;
        self.message = message;

        self.alertLayer = [[CALayer alloc] init];
    
        self.layer.cornerRadius = kCORNER_RADIUS;
        self.layer.shadowRadius = 3.0;
        self.layer.shadowColor = [UIColor blackColor].CGColor;
        self.layer.shadowOffset = CGSizeMake(15, 20);
        self.layer.shadowOpacity = 1.0;

        self.alertLayer.delegate = self;
        self.alertLayer.masksToBounds = YES;
        self.alertLayer.cornerRadius = kCORNER_RADIUS;

        [self.layer addSublayer:self.alertLayer]; // This line of code seems to cause EXC_BAD_ACCESS
    }

    return self;
}

EXC_BAD_ACCESS 是在视图控制器或 UIWindow 中调用 [self.view addSubview:alertView] 后引起的。

【问题讨论】:

  • alertLayer 属性使用什么内存管理语义?

标签: ios uiview calayer quartz-2d uiwindow


【解决方案1】:

您有两个层(self.layerself.alertLayer)具有相同的委托 self,当将此视图(self)添加到视图树时,这会导致内部方法 -[UIView(Hierarchy) _makeSubtreePerformSelector:withObject:withObject:copySublayers:] 中的无限递归.因此,您必须删除 self.alertLayer.delegate = self; 以避免崩溃。如果您需要为alarmLayer 委托,您可以创建不同的对象。

【讨论】:

  • 谢谢,我不知道 UIView 只能有一个层作为委托引用它。
猜你喜欢
  • 1970-01-01
  • 2013-12-13
  • 1970-01-01
  • 2023-04-05
  • 2016-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多