2011-08-03 31 views
0

我單身全球一流的具有都在同一類這些categeories,在頭文件中的接口和implemantation提交其實現如下,帶有類別的iPhone全球課,這是正確的嗎?

這工作,但我不太清楚我在做什麼是一個好習慣這裏是因爲它已經是一個單例類了,我將這些方法稱爲類別並執行一些分配,釋放......以及其他東西。或者他們在全球一級之外是因爲他們是不同的實現?

你能告訴我一切是否正確嗎?

#import "GlobalConfig.h" 
#import "SynthesizeSingleton.h" 
@implementation GlobalConfig 
SYNTHESIZE_SINGLETON_FOR_CLASS(GlobalConfig); 
@end 

@implementation UIColor (APP) 
+(UIColor *) APP_NAV_COLOR { return [UIColor colorWithRed:00/256.0 green:111/256.0  
               blue:59/256.0 alpha:1.0]; } 
+(UIColor *) APP_BUTTON_COLOR { return [UIColor colorWithRed:00/256.0 green:00/256.0  
@end 

@implementation UIImage(APP) 
+(UIImage *) APP_IMAGE_BCKGROUND { 

NSString *path = [[NSBundle mainBundle] pathForResource:@"bckPhone" ofType:@"png"]; 
return [UIImage imageWithContentsOfResolutionIndependentFile:path]; 
} 
@end 

@implementation CALayer(APP) 
+(CALayer *) APP_SELECTION_VIEW:(UITableViewCell *)cell { 
UIView *cellView = [[UIView alloc] initWithFrame:cell.frame]; 
CAGradientLayer *gradient = [CAGradientLayer layer]; 
gradient.frame = cellView.bounds; 
gradient.startPoint = CGPointMake(0.5, 0); 
gradient.endPoint = CGPointMake(0.5, 1); 
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor APP_WHITE_COLOR] CGColor], (id)[[UIColor APP_BLACK_COLOR] CGColor], nil]; 
[cellView release]; 
return gradient; 
} 
@end 

@implementation UIView(APP) 
+(UIView *) APP_BACKGROUND_VIEW:(UITableViewCell *)cell { 
UIView *cellView2 = [[UIView alloc] initWithFrame:cell.frame]; 
CAGradientLayer *gradient2 = [CAGradientLayer layer]; 
gradient2.frame = cellView2.bounds; 
gradient2.startPoint = CGPointMake(0, 0.5); 
gradient2.endPoint = CGPointMake(1, 0.5); 
gradient2.colors = [NSArray arrayWithObjects:(id)[[UIColor APP_WHITE_COLOR] CGColor], (id)[[UIColor APP_COLOR] CGColor], nil]; 
[cell.layer insertSublayer:gradient2 atIndex:0]; 
[cellView2 release]; 
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame]; 
myBackView.backgroundColor=[[UIColor APP_COLOR] colorWithAlphaComponent:0.1]; 
[myBackView autorelease]; 
return myBackView; 
} 
@end 

回答

1

單獨的文件現有類(即,CALayer,UIImageUIColor)通過類別,您不需要單身。

單例是一個用於實例化單個對象(並且具有唯一保證)的類。類別是擴展現有接口的一種方式,不需要添加任何ivars(僅限於方法),因此在使用它之前不需要實例化類別。所以類別和單例對不同的需求作出響應,不需要混合它們。

您可以像您一樣簡單地定義類別,然後將它們與您的UIView,UIColor和CALAyer對象一起使用。

+0

tnx我應該創建另一個非單實例的utiliy類,並將其中的所有類別? – Spring

+1

如果您願意,您可以創建一個實用程序文件,並將所有類別(每個類別都視爲類/接口語法)。然後,當您使用它們時,您將立即導入所有這些文件。或者你可以有單獨的文件,所以你可以一個一個地導入它們。 – sergio

+0

我剛剛將它們移到另一個類中,並且我收到了其他正在使用它們的類的警告,因爲它們可能不會響應該方法?我是否必須手動將該類導入到每個我使用的類中?並且不需要實例化我猜? – Spring

1

那麼單身人士課程是打算或爲班級創建一個實例並在整個生命週期中使用相同的實例。

這兩個條件中的任何一個都不安全 1.只要你在GlobalConfig.h文件中有子類的接口聲明。 2.如果您有任何的子類/擴展使用GlobalConfig的共享單一對象

,所以我認爲它總是更好。如果要擴展到創造這樣的UIView + AppExtensions,UIImage的+ AppExtensions

+0

tnx我應該創建另一個非單實例的utiliy類,並將其中的所有類別?我應該什麼時候打電話給那個班? – Spring