Objective-C 使用 typedef enum 来设置类行为,就像 Cocoa 一样

发布于 2024-08-09 02:52:48 字数 1572 浏览 4 评论 0原文

我扩展 UIButton 类以便能够设置 UINavigationBarButton 的字体和颜色(从此代码示例中: 打开代码

我是这样的:

@interface NavBarButtonGrey : UIButton 
-(id)init;

@end


@implementation NavBarButtonGrey

-(id)init {
if(self = [super init]) {
    self.frame = CGRectMake(0, 0, 49.0, 30.0);
    self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

    UIImage *image = [UIImage imageNamed:@"greyNavButton.png"];
    UIImage *stretchImage = 
    [image stretchableImageWithLeftCapWidth:15.0 topCapHeight:0.0];
    [self setBackgroundImage:stretchImage forState:UIControlStateNormal];
    self.backgroundColor = [UIColor clearColor];
    [self setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.titleShadowOffset = CGSizeMake(0, -1);
    self.titleLabel.font = [UIFont boldSystemFontOfSize:13];
}

return self;
}
@end

这没问题,但当然不是很灵活。 我如何使用 typedef 枚举(就像苹果所做的那样)来合并所有不同的 我希望自定义按钮符合的颜色、字体和大小。

我可以从 UIKit 的界面文件中得到的唯一东西是它是这样完成的:

typedef enum {
RGCustomNavBarButtonStyleBlue,
RGCustomNavBarButtonStyleGrey,
RGCustomNavBarButtonStyleBlack,
RGCustomNavBarButtonStyleGreen,
RGCustomNavBarButtonStyleRed, 
} RGCustomNavBarButtonStyle;

如何从中获取并进入一个工作实现,该实现通过构造函数从枚举的值获取字体、大小、颜色等( initWithStyle)?

Objective C 中是否重载构造函数?多个构造函数?

希望这是有道理的,并感谢您提供的任何帮助:)

Im extending the UIButton Class to be able to set the font and color of the UINavigationBarButton ( from this code example: switch on the code )

I goes like this:

@interface NavBarButtonGrey : UIButton 
-(id)init;

@end


@implementation NavBarButtonGrey

-(id)init {
if(self = [super init]) {
    self.frame = CGRectMake(0, 0, 49.0, 30.0);
    self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

    UIImage *image = [UIImage imageNamed:@"greyNavButton.png"];
    UIImage *stretchImage = 
    [image stretchableImageWithLeftCapWidth:15.0 topCapHeight:0.0];
    [self setBackgroundImage:stretchImage forState:UIControlStateNormal];
    self.backgroundColor = [UIColor clearColor];
    [self setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.titleShadowOffset = CGSizeMake(0, -1);
    self.titleLabel.font = [UIFont boldSystemFontOfSize:13];
}

return self;
}
@end

This is ok, but of course not very flexible.
How do I incorporate using a typedef enum (like Apple does) for all the different
colors, fonts and sizes I would like my custom button to conform to.

The only thing I can get out of the interface files from UIKit is that it is done like this:

typedef enum {
RGCustomNavBarButtonStyleBlue,
RGCustomNavBarButtonStyleGrey,
RGCustomNavBarButtonStyleBlack,
RGCustomNavBarButtonStyleGreen,
RGCustomNavBarButtonStyleRed, 
} RGCustomNavBarButtonStyle;

How to get from that and into a working implementation that takes font, size, color etc. from the values of the enum through the constructor(initWithStyle)?

Does one overload constructors in Objective C? multiple constructors?

Hope it makes sense and thank you for any help given:)

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

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

发布评论

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

评论(2

深海不蓝 2024-08-16 02:52:48

为了扩展上面 ennukiller 所说的内容,我被教导(Hillegass 的书)选择一个初始化程序 - 通常是具有最多选项的初始化程序,例如 initWithFont:andColor: - 并让其他初始化程序调用它。该主初始化程序称为指定初始化程序。

所以你的代码将有一个完全实现的 initWithFont:andColor: 调用 [super init],然后你还有一个 initWithFont: 看起来像这样:

-(MyClass) initWithFont: (UIFont) font
{
    [self initWithFont:font andColor:RGCustomNavBarButtonStyleBlack];
}

然后你的 initWithFont:andColor: 将处理所有其他设置并调用[超级初始化]。

To expand on what ennuikiller said above, I was taught (Hillegass's book) to pick one initializer—usually the one with the most options, like your initWithFont:andColor:—and have the other initializers call it. That main initializer is referred to as the designated initializer.

So your code would have a fully-implemented initWithFont:andColor: that calls [super init], and then you'd also have an initWithFont: that looks something like this:

-(MyClass) initWithFont: (UIFont) font
{
    [self initWithFont:font andColor:RGCustomNavBarButtonStyleBlack];
}

Then your initWithFont:andColor: would handle all the other setup and calling [super init].

狼亦尘 2024-08-16 02:52:48

您可以有多个构造函数,例如;

-(MyClass) initWithFont: (UIFont) font;
-(MyClass) initWithFonmt: (UIFont) font andColor: (UIColor) color;

等等。

然后调用 [super init] 作为每个自定义构造函数中的第一行。

You can have multiple constructors such as;

-(MyClass) initWithFont: (UIFont) font;
-(MyClass) initWithFonmt: (UIFont) font andColor: (UIColor) color;

etc.

Then call [super init] as the first line in each of your custom constructors.

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