在iOS 7和Objective c中使用UIColor类别
我有一个UIColor+MyLayout.m文件,例如:在iOS 7和Objective c中使用UIColor类别,ios,objective-c,ios7,objective-c-category,Ios,Objective C,Ios7,Objective C Category,我有一个UIColor+MyLayout.m文件,例如: @implementation UIColor (Layout) - (UIColor *) textBackground { UIColor *lightGreen = [UIColor colorWithRed:0.0f/255.0f green:178.0f/255.0f blue:238.0f/255.0f alpha:1.0f]; return lightGreen; } @end 我已将.h文件
@implementation UIColor (Layout)
- (UIColor *) textBackground
{
UIColor *lightGreen = [UIColor colorWithRed:0.0f/255.0f green:178.0f/255.0f blue:238.0f/255.0f alpha:1.0f];
return lightGreen;
}
@end
我已将.h文件添加到我的viewcontroller.m中,但如何将其称为UIColor
UIColor*myColor=?您可以在签名中使用
+
而不是-
将textBackground
转换为类方法,然后只需:
UIColor *myColor = [UIColor textBackground];
您可以通过在签名中使用
+
而不是-
将textBackground
转换为类方法,然后简单地:
UIColor *myColor = [UIColor textBackground];
如果您执行以下操作会更好:
@implementation UIColor (Layout)
+ (UIColor *) textBackground {
UIColor *lightGreen = [UIColor colorWithRed:0.0f/255.0f green:178.0f/255.0f blue:238.0f/255.0f alpha:1.0f];
return lightGreen;
}
@end
然后把它叫做
UIColor*myColor=[UIColor textBackground]代码>如果您执行以下操作会更好:
@implementation UIColor (Layout)
+ (UIColor *) textBackground {
UIColor *lightGreen = [UIColor colorWithRed:0.0f/255.0f green:178.0f/255.0f blue:238.0f/255.0f alpha:1.0f];
return lightGreen;
}
@end
然后把它叫做UIColor*myColor=[UIColor textBackground]代码>首先,您必须在类中导入类别文件,如:
#import "UIColor+Layout.h"
然后您需要将此方法调用为
[UIColor textBackground]
此外,您还需要将您的类别方法设置为类方法首先,您必须在类中导入类别文件,如:
#import "UIColor+Layout.h"
然后您需要将此方法调用为
[UIColor textBackground]
此外,您还需要将您的category方法设置为类方法您应该将此方法设置为静态方法,如
@implementation UIColor (Layout)
+ (UIColor *) textBackground {
UIColor *lightGreen = [UIColor colorWithRed:0.0f/255.0f green:178.0f/255.0f blue:238.0f/255.0f alpha:1.0f];
return lightGreen;
}
@end
然后用类名调用它,比如
UIColor*myColor=[UIColor textBackground]代码>
您应该导入UIColor+MyLayout.h
#导入UIColor+MyLayout.h
您应该使此方法像
@implementation UIColor (Layout)
+ (UIColor *) textBackground {
UIColor *lightGreen = [UIColor colorWithRed:0.0f/255.0f green:178.0f/255.0f blue:238.0f/255.0f alpha:1.0f];
return lightGreen;
}
@end
然后用类名调用它,比如
UIColor*myColor=[UIColor textBackground]代码>
您应该导入UIColor+MyLayout.h
#导入UIColor+MyLayout.h
试试这个。。。。它正在工作强>
1.将UIColor
的子类命名为vv
因此,在UIColor+vv.h
#import <UIKit/UIKit.h>
@interface UIColor (vv)
+(UIColor*)mh;
@end
ViewController.m
#import "UIColor+vv.h"
@implementation UIColor (vv)
+(UIColor*)mh
{
UIColor *lightGreen = [UIColor colorWithRed:0.0f/255.0f green:178.0f/255.0f blue:238.0f/255.0f alpha:1.0f];
return lightGreen;
}
@end
#import "UIColor+vv.h"
- (void)viewDidLoad
{
lbl.textColor=[UIColor mh];
}
如果您有任何问题,请告诉我。
试试这个。。。。它正在工作强>
1.将UIColor
的子类命名为vv
因此,在UIColor+vv.h
#import <UIKit/UIKit.h>
@interface UIColor (vv)
+(UIColor*)mh;
@end
ViewController.m
#import "UIColor+vv.h"
@implementation UIColor (vv)
+(UIColor*)mh
{
UIColor *lightGreen = [UIColor colorWithRed:0.0f/255.0f green:178.0f/255.0f blue:238.0f/255.0f alpha:1.0f];
return lightGreen;
}
@end
#import "UIColor+vv.h"
- (void)viewDidLoad
{
lbl.textColor=[UIColor mh];
}
如果您有任何问题,请告诉我。您想要的是+(UIColor*)textBackground
而不是-(UIColor*)textBackground
。然后,您可以简单地在提供的答案中使用它。您想要的是+(UIColor*)textBackground
而不是-(UIColor*)textBackground
。然后,您可以简单地按照提供的答案使用它。您必须生成UIColor
的子类,而不是layouput
。这对您有帮助吗?如果你尝试这个&遇到任何问题,请告诉我。它对我有用。为什么要重复现有的答案?为什么会有这么大的截图?@MartinR,我不会重复同样的答案。看看其他答案。他们正在制作Layout
的子类,而不是UIColor
&其他答案不被接受,因此我尝试用以下代码显示输出。您必须生成UIColor
的子类,而不是layouput
。这对您有帮助吗?如果你尝试这个&遇到任何问题,请告诉我。它对我有用。为什么要重复现有的答案?为什么会有这么大的截图?@MartinR,我不会重复同样的答案。看看其他答案。他们正在制作Layout
的子类,而不是UIColor
&其他答案不被接受,因此我尝试用以下代码显示输出。他还需要导入类别。他还需要导入类别。这是一个类方法,而不是静态方法。事实上,上面也给出了同样的答案。静态方法实际上是一种类方法,它是一种类方法,而不是静态方法。事实上,上面也给出了同样的答案。静态方法实际上是类方法