gpt4 book ai didi

ios - 如何在 View 的 initWithCoder 方法中访问 subview

转载 作者:行者123 更新时间:2023-11-28 18:39:41 25 4
gpt4 key购买 nike

我有一个包含两个 UILabel 的自定义 View 。我之前想自定义他们的字体,所以我在 initWithCoder 方法中这样做了。

@implementation HomeTitleView
@synthesize ticketLabel;
@synthesize monthLabel;

- (id) initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if (self) {
[monthLabel setFont:[UIFactory getFontForKey:@"home_month"]];
[ticketLabel setFont:[UIFactory getFontForKey:@"home_ticket"]];
}
return self;
}

@end

不幸的是,这没有用。使用调试器,我发现 monthLabel 和 ticketLabel 都是 nil。任何人都知道我该如何解决这个问题?我应该实现什么回调或方法才能访问我的两个标签?

最佳答案

你不能那样做。这些 View 尚不存在。它们在调用 loadView 方法时实例化,这在首次访问 view 属性时自动发生。如果您想在 View 加载后对其进行操作,正确的方法是使用 viewDidLoad

编辑:假设您正在使用 UIViewController 类。如果您使用的是 UIView 类,则可以使用 awakeFromNibdidAddSubview:

关于ios - 如何在 View 的 initWithCoder 方法中访问 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12771798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com