我正在使用最新的 SDK 开发 iOS 应用。
我创建了一个继承自 UIView
的类,每次实例化该类时我都必须进行一些初始化。
我必须调用一个名为 setUpVars:
的方法,但我不知道向该方法发送消息的位置:
- (id)initWithFrame:(CGRect)frame;
- (id)initWithCoder:(NSCoder*)aDecoder;
此类可与自定义 xib 一起使用,或添加到 Storyboard,因此我需要确保在每种情况下都会调用该方法。
- (void)setUpVars
{
_preferenceKey = @"";
_preferenceStatus = NO;
_isDown = NO;
}
我必须在哪里添加 [self setUpVars];
?
最佳答案
基本上你会想要涵盖这两种情况
- (id)initWithFrame:(CGRect)frame;
{
self = [super initWithFrame:frame];
if (self) {
[self setUpVars];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder;
{
self = [super initWithCoder:aDecoder];
if (self) {
[self setUpVars];
}
return self;
}
关于ios - 具有自定义初始化的自定义 UIView 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15357181/