gpt4 book ai didi

objective-c - 样式 UIButtons - 子类?

转载 作者:可可西里 更新时间:2023-11-01 05:06:45 25 4
gpt4 key购买 nike

我有四个按钮,它们将在同一场景中以相同的方式设置样式(使用 Storyboard)。这是一个简单的样式,需要覆盖一些属性默认值,但似乎不必要地重复为每个单独的按钮设置它。我想我会创建一个子类,但我读过的很多帖子(尤其是关于 stackoverflow 的帖子)都警告不要为 UIButton 这样做(而且我所做的尝试都没有成功)。

只是希望得到一个关于什么被认为是最好的方法的一般指示。感谢您的任何建议。

最佳答案

如果您只针对 iOS 5,我强烈建议您观看 Session 114 - Customizing the Appearance of UIKit Controls WWDC 2011 session 视频需要开发人员登录

它详细解释了应用范围内的样式。


我想修改yuji的思路:使用一个category在 UIButton 上设置按钮

.h.

@interface UIButton (MyStyling)
-(void)configureMyButtonStyle;
//other methods for more fine-grained control
@end

.m

@implementation UIButton (MyStyling)
-(void)configureMyButtonStyle
{
[self setBackgroundColor:[UIColor colorWithRed:…]];
[self setTitleColor: [UIColor colorWithRed:…] forState: UIControlStateNormal];
//…

}
@end

现在你可以调用[aButton configureMyButtonStyle]

当然你也可以在一些参数中解析,来区分几种风格。

-(void)configureMyButtonForStyle:(NSInteger)style
{
if(style == 1){
//…
} else if(style == 2) {
//..
} else {
//fallback style
}
}

使用:

[aButton configureMyButtonForStyle:1];

关于objective-c - 样式 UIButtons - 子类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9403383/

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