Iphone Objective-C使用typedef枚举设置类行为,如Cocoa
我正在扩展UIButton类,以便能够设置UINavigationBarButton的字体和颜色(来自此代码示例:) 我是这样说的:Iphone Objective-C使用typedef枚举设置类行为,如Cocoa,iphone,uikit,enums,uibutton,typedef,Iphone,Uikit,Enums,Uibutton,Typedef,我正在扩展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); s
@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 enum {
RGCustomNavBarButtonStyleBlue,
RGCustomNavBarButtonStyleGrey,
RGCustomNavBarButtonStyleBlack,
RGCustomNavBarButtonStyleGreen,
RGCustomNavBarButtonStyleRed,
} RGCustomNavBarButtonStyle;
-(MyClass) initWithFont: (UIFont) font
{
[self initWithFont:font andColor:RGCustomNavBarButtonStyleBlack];
}
这是可以的,但当然不是很灵活。
我如何使用typedef枚举(像Apple那样)来整合所有不同的应用程序
颜色,字体和大小,我希望我的自定义按钮符合
我能从UIKit的接口文件中得到的唯一一件事是,它是这样做的:
@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 enum {
RGCustomNavBarButtonStyleBlue,
RGCustomNavBarButtonStyleGrey,
RGCustomNavBarButtonStyleBlack,
RGCustomNavBarButtonStyleGreen,
RGCustomNavBarButtonStyleRed,
} RGCustomNavBarButtonStyle;
-(MyClass) initWithFont: (UIFont) font
{
[self initWithFont:font andColor:RGCustomNavBarButtonStyleBlack];
}
如何从中获得一个工作实现,该实现通过构造函数(initWithStyle)从枚举的值中获取字体、大小、颜色等
在目标C中是否有一个构造函数过载?多个构造函数
希望它有意义,并感谢您提供的任何帮助:)您可以有多个构造函数,例如
-(MyClass) initWithFont: (UIFont) font;
-(MyClass) initWithFonmt: (UIFont) font andColor: (UIColor) color;
等等
然后调用[super init]作为每个自定义构造函数的第一行。要扩展ennuikiller上面所说的内容,我被教导(Hillegass的书)选择一个初始值设定项,通常是选项最多的初始值设定项,如initWithFont:和Color:-并让其他初始值设定项调用它。该主初始值设定项称为指定初始值设定项 因此,您的代码将有一个完全实现的initWithFont:andColor:调用[super init],然后您还将有一个initWithFont:看起来像这样:
@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 enum {
RGCustomNavBarButtonStyleBlue,
RGCustomNavBarButtonStyleGrey,
RGCustomNavBarButtonStyleBlack,
RGCustomNavBarButtonStyleGreen,
RGCustomNavBarButtonStyleRed,
} RGCustomNavBarButtonStyle;
-(MyClass) initWithFont: (UIFont) font
{
[self initWithFont:font andColor:RGCustomNavBarButtonStyleBlack];
}
然后,您的initWithFont:andColor:将处理所有其他设置并调用[super init]。首先,UINavigationBarButton不存在。您可能指的是UIBarbuttonite,它继承自UIBarItem,反过来又继承自NSObject。子类化UIButton在这方面对您没有帮助。抱歉,我是说UIButton:)我是这样做的:NavBarButtonGrey*addButton=[[NavBarButtonGrey alloc]init];[addButton设置标题:@“添加”状态:UIControlStateNormal];[addButton addTarget:self action:@selector(addStuff)for ControlEvents:UIControlEventTouchUpInside];UIBarButtonItem*addNavButton=[[UIBarButtonItem alloc]initWithCustomView:addButton];self.navigationItem.rightBarButtonItem=addNavButton;用我定制的NavBarButtonGrey,效果很好?我找不到给uibarbuttonein上色的方法,所以我决定试着做一个定制的,可以定制的。谢谢:)看起来不错。这样我就可以用字体(RGCustomNavBarButtonStyle)初始化-(MyClass)字体;并将其作为字体传递给“RGCustomNavBarButtonStyleGreen”,因为我基本上是用typedef创建自己的类型。然后在类中有一个开关盒,在返回UIButton类型的按钮之前定义参数。太好了:)再次感谢!嗨,杰。感谢您详细介绍初始值设定项主题。这个问题到现在已经有一个月了,从那以后我学到了一些应该做和不应该做的事情。我也读到,这是一本客观C语言书中的首选方法。我发现自己越来越多地使用typedef enum/多个初始值设定项,Objective C中的命名约定非常棒。鼓励使用以下哪种语言命名:shouldRemoveViewContainingRedBackgroundFromSuperView:)