nib 中的自定义视图实例化为父类型

发布于 2024-09-04 21:37:35 字数 156 浏览 9 评论 0原文

我创建了一个继承自 UILabel 的自定义类。我在笔尖中放置了一个 UILabel 并将标签的类设置为我的自定义类。但是,当调用 viewDidLoad 方法时,视图属于 UILabel 类,而不是我的自定义类。

有什么想法吗?

I've created a custom class inheriting from UILabel. I placed a UILabel in my nib and set the class of the label to my custom class. However, when the viewDidLoad method is called, the view is of the class UILabel instead of my custom class.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

り繁华旳梦境 2024-09-11 21:37:36

我能够在测试项目中创建 CustomLabel 的实例。我所做的不同之处在于,我没有拖动 UILabel,而是将 CustomLabel 的实例拉入视图中。

我针对最新 sdk 的目标实现了 awakeFromNib,如下所示。

-(void)awakeFromNib
{
    [self setText:[NSString stringWithFormat:@"%@",[self class]]];
}

- (void)drawTextInRect:(CGRect)rect // called as expected
{
    NSLog(@"drawTextInRect: called");
    [super drawTextInRect:rect];
}

几个谜团...
我在 CustomLabel 中实现了 init 和 initWithFrame,从笔尖加载视图的机制都没有调用它们。

I was able to create an instance of my CustomLabel in a test project. What I did differently was instead of dragging a UILabel, I pulled in an instance of CustomLabel into the view.

I implemented awakeFromNib against a target for the latest sdk as follows.

-(void)awakeFromNib
{
    [self setText:[NSString stringWithFormat:@"%@",[self class]]];
}

- (void)drawTextInRect:(CGRect)rect // called as expected
{
    NSLog(@"drawTextInRect: called");
    [super drawTextInRect:rect];
}

Couple of mysteries...
I implemented init and initWithFrame in CustomLabel, neither was called by the machinery that loads the view from the nib.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文