Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在iOS 7和Objective c中使用UIColor类别_Ios_Objective C_Ios7_Objective C Category - Fatal编程技术网

在iOS 7和Objective c中使用UIColor类别

在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文件

我有一个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文件添加到我的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
&其他答案不被接受,因此我尝试用以下代码显示输出。他还需要导入类别。他还需要导入类别。这是一个类方法,而不是静态方法。事实上,上面也给出了同样的答案。静态方法实际上是一种类方法,它是一种类方法,而不是静态方法。事实上,上面也给出了同样的答案。静态方法实际上是类方法