'self'时使用的实例变量未设置为[(super或self)init ...]的结果

时间:2016-03-17 06:47:32

标签: ios memory-leaks

我的代码:

- (instancetype)initWithSender:(UIView*)sender withSuperView:(UIView*)superView withItems:(NSMutableArray*)items
{
    self = [CustomDropdownView loadInstanceFromNibWithiPad:NO];
    if (self) {
        self.frame = CGRectZero;

        [self.layer setBorderColor:[UIColor lightGrayColor].CGColor];
        [self.layer setBorderWidth:1.0];

        CGRect positionInSuper = [superView convertRect:sender.frame fromView:sender.superview];
        float height = superView.frame.size.height - positionInSuper.origin.y;
        if (height > 200.0) {
//            height = 200.0;
        }

        [superView addSubview:self];
        self.translatesAutoresizingMaskIntoConstraints = NO;

        self.constLeading = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:positionInSuper.origin.x];
        self.constTop = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeTop multiplier:1.0 constant:positionInSuper.origin.y + sender.frame.size.height];

        self.constWidth = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:sender.frame.size.width];
        self.constHeight = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:0];

        [superView addConstraints:@[self.constLeading, self.constTop]];
        [self addConstraints:@[self.constWidth, self.constHeight]];

        [self layoutIfNeeded];

        self.arrItems = [[NSMutableArray alloc]initWithArray:items];

        [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([DropDownTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([DropDownTableViewCell class])];

        [self.tableView reloadData];
    }
    return self;
}

1 个答案:

答案 0 :(得分:0)

第一行

    self = [CustomDropdownView loadInstanceFromNibWithiPad:NO];

非常有问题。您应该只分配另一个init方法的结果,通常是[super init]。将其更改为类方法。

顺便说一下,这是错误信息所说的内容。