2014-02-21 40 views
-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' 

我所試圖做的是創造,我可以使用,能夠落實到每一個視圖控制器來改變它的背景顏色爲這個定製的十六進制顏色。

建議,想法?

+0

看看你做了什麼。你有兩種方法合併在一起的奇怪混搭。像任何其他方法一樣創建兩個獨立的方法。 – rmaddy

+0

你已經拼出了兩個不同方式的類別名稱。他們必須完全匹配! – matt

+0

這裏檢查更新我改變了它... –

回答

1

看來colorWithHexString應該是一個類方法,而不是一個實例方法。 更改爲:

+ (UIColor*)colorWithHexString:(NSString*)hex { 

    ... 
}