Objective-C 使用 typedef enum 来设置类行为,就像 Cocoa 一样
我扩展 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了扩展上面 ennukiller 所说的内容,我被教导(Hillegass 的书)选择一个初始化程序 - 通常是具有最多选项的初始化程序,例如 initWithFont:andColor: - 并让其他初始化程序调用它。该主初始化程序称为指定初始化程序。
所以你的代码将有一个完全实现的 initWithFont:andColor: 调用 [super init],然后你还有一个 initWithFont: 看起来像这样:
然后你的 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:
Then your initWithFont:andColor: would handle all the other setup and calling [super init].
您可以有多个构造函数,例如;
等等。
然后调用 [super init] 作为每个自定义构造函数中的第一行。
You can have multiple constructors such as;
etc.
Then call [super init] as the first line in each of your custom constructors.