OGeek|极客世界-中国程序员成长平台

标题: ios - 具有自定义初始化的自定义 UIView 不起作用 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 09:33
标题: ios - 具有自定义初始化的自定义 UIView 不起作用

我正在使用最新的 SDK 开发 iOS 应用。

我创建了一个继承自 UIView 的类,每次实例化该类时我都必须进行一些初始化。

我必须调用一个名为 setUpVars: 的方法,但我不知道在哪里向该方法发送消息:

- (id)initWithFrameCGRect)frame;
- (id)initWithCoderNSCoder*)aDecoder;

此类可以与自定义 xib 一起使用,或添加到 Storyboard,因此我需要确保在每种情况下都会调用该方法。

- (void)setUpVars
{
    _preferenceKey = @"";
    _preferenceStatus = NO;
    _isDown = NO;
}

我必须在哪里添加 [self setUpVars];



Best Answer-推荐答案


基本上你会想要涵盖这两种情况

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

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

关于ios - 具有自定义初始化的自定义 UIView 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15357181/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4