-2
我無法將我的自定義uicolor
添加自定義類別 - 的OBJç
我有一個.h
和.m
文件,並重命名爲custcolors
這裏是我的custColors.h
文件看起來像:
#import <UIKit/UIKit.h>
@interface UIColor (MyCatagory)
+ (UIColor*) customRedColor;
@end
這裏是我的custColors.m
文件看起來像:
#import "custColors.h"
@implementation UIColor (MyCategory)
+ (UIColor *)customRedColor {
return [UIColor colorWithHexString:@"88C800"];
}
-(UIColor*)colorWithHexString:(NSString*)hex
{
NSString *cString = [[hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
// String should be 6 or 8 characters
if ([cString length] < 6) return [UIColor grayColor];
// strip 0X if it appears
if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
if ([cString length] != 6) return [UIColor grayColor];
// Separate into r, g, b substrings
NSRange range;
range.location = 0;
range.length = 2;
NSString *rString = [cString substringWithRange:range];
range.location = 2;
NSString *gString = [cString substringWithRange:range];
range.location = 4;
NSString *bString = [cString substringWithRange:range];
// Scan values
unsigned int r, g, b;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
return [UIColor colorWithRed:((float) r/255.0f)
green:((float) g/255.0f)
blue:((float) b/255.0f)
alpha:1.0f];
}
@end
我的問題是,當我嘗試使用這個,我只是得到一個錯誤。話說;
custColors.h:12:33: Expected identifier
custColors.h:12:35: Expected an Objective-C directive after '@'
custColors.m
custColors.m:13:51: Use of undeclared identifier 'hex'
我所試圖做的是創造,我可以使用,能夠落實到每一個視圖控制器來改變它的背景顏色爲這個定製的十六進制顏色。
建議,想法?
看看你做了什麼。你有兩種方法合併在一起的奇怪混搭。像任何其他方法一樣創建兩個獨立的方法。 – rmaddy
你已經拼出了兩個不同方式的類別名稱。他們必須完全匹配! – matt
這裏檢查更新我改變了它... –