1

我正在处理继承的代码库并尝试解决以下警告:

指定的初始化程序应该只在“super”上调用指定的初始化程序

指定的初始化程序缺少对超类的指定初始化程序的“超级”调用

代码是:

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [self initWithFrame:[CDCUtility getScreenBounds]]; //switching to super breaks
    if (self) {
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setArray:[NSMutableArray new]];
        [self setButtonArray:[NSMutableArray new]];
        _graphicEQ = [[CDCEffectsGraphicEQ alloc] initWithFrame:CGRectMake((1024 / 2) - (811 / 2), 60, 860, 255)];
        [self addSubview:_graphicEQ];
        [_graphicEQ setDelegate:self];
        [self addBypassButtonToView];
        [self addFlatButtonToView];
        [self addScrollView];
    }
    return self;
}

因此,据我所知,开发人员覆盖了超类initWithCoder:(This is a UIView)以允许加载自定义 UI,并initWithFrame:使用自定义参数传入此参数以创建视图。

我看到有人说将[self initWithFrame:]'更改initWithCoder:[super initWithFrame:]确实可以解决警告,但是它也绕过了调用此处所需的功能以正确加载视图。

它按原样工作正常;我只是想减少所有可能的警告,所以我想知道是否可以进行更改来解决这个问题?

4

1 回答 1

1

尝试这个:

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self designatedInitializer];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self designatedInitializer];
    }
    return self;
}

- (void) designatedInitializer {
    [self setArray:[NSMutableArray new]];
    [self setButtonArray:[NSMutableArray new]];
    _graphicEQ = [[CDCEffectsGraphicEQ alloc] initWithFrame:CGRectMake((1024 / 2) - (811 / 2), 60, 860, 255)];
    [self addSubview:_graphicEQ];
    [_graphicEQ setDelegate:self];
    [self addBypassButtonToView];
    [self addFlatButtonToView];
    [self addScrollView];
}
于 2017-04-21T11:30:34.177 回答