gpt4 book ai didi

iOS 应用程序 : UIButton tag not available in controller code

转载 作者:行者123 更新时间:2023-11-28 22:22:13 24 4
gpt4 key购买 nike

我写了一个方法来改变viewcontroller所有控件的字体。该方法获取 View Controller 的 Root View 并递归迭代所有 View 以更改字体。为了了解必须应用于文本的文本样式,我将 Interface Builder 中的代码设置为每个控件的标记属性。除 UIButton 实异常(exception),一切正常。 UIButtons 没有 IBOutlet,因为我不需要在代码中对它们进行任何显式引用。问题是我的 Controller 无法使用以下方法检索按钮标签值:

int viewTag = [view tag];

无论我在 Interface Builder 中设置什么,它始终为 0。为什么?

这里是我使用的代码片段:

字体管理类

+ (void)setCustomFontToView:(UIView *)view {

if ( [view isKindOfClass:[UILabel class]] &&
[view respondsToSelector:@selector(setFont:)] ) {

int viewTag = [view tag];

UIFont *font = nil;

if ( viewTag == 1 ) {

font = [UIFont fontWithName:@"Frutiger-Light" size:18];

} else if ( viewTag == 2 ) {

font = [UIFont fontWithName:@"Frutiger-Bold" size:20];
}

[(UILabel *) view setFont:font];
}

NSArray *childViews = [view subviews];

if ( [childViews count] > 0 ) {

for ( UIView *childView in childViews ) {
[self setCustomFontToView:childView];
}
}
}

View Controller

- (void)viewDidLoad
{
.....
[FontManager setCustomFontToView:self.view];
}

最佳答案

我认为您是为 UIButton 设置标记值,而不是为它的子项 UILabel 设置标记值。这就是它总是返回 0 的原因。添加对 UIButton 和标签的检查,而不是将字体设置为标题标签。

关于iOS 应用程序 : UIButton tag not available in controller code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20025028/

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