2012-07-30 109 views
0

我想擴展UIButtonNSString一些元信息。我怎麼能這樣做?添加NSString類別擴展到UIButton

我開始與此:

@interface UIButton (neoUtils) 

+ (UIButton *)neoName:(NSString *)buttonName; 

@end 

與.m

#import "UIButton+NAME.h" 

@implementation UIButton (neoUtils) 

+ (UIButton *)neoName:(NSString *)buttonName { 
    UIButton *button = [UIButton neoName:buttonName]; 
    NSLog(@"%@",button); 
    return button; 
} 

@end 

這是在正確的道路上?如果是這樣 - 我將如何使用它?

+0

這個問題不是重複的,但它已經在不同的上下文中回答過。請參閱http://stackoverflow.com/questions/10414718/subclassing-nsnumber。 – 2012-07-31 22:27:47

+0

這似乎是我正在尋找。謝謝 – malaki1974 2012-08-01 13:04:40

回答

1

我假設

@interface UIButton (neoUtils) 

中的UIButton + NAME.h聲明。

首先,看起來就像當你調用該方法,方案將盡快進入一個遞歸循環:

+ (UIButton *)neoName:(NSString *)buttonName { 
    UIButton *button = [UIButton neoName:buttonName]; // <- this one 
    NSLog(@"%@",button); 
    return button; 
} 

,因爲它會recurevely調用該方法本身。

無論如何,考慮到擴展對象必須有一個狀態(元信息的NSString必須「記住」)我不相信有可能滿足一個類別的要求,這只是擴展了行爲班上。那麼你的解決方案並沒有從我猜想的正確步驟開始。

相反,我只是vould創建一類像

@interface XYMetaInfoButton : UIButton 

@proerty (nonatomic, [strong|retain]) NSString *name; 

@end 

,然後可以在項目中有新的UIButton與元信息導入全球。 但這只是一種可能性,也許有人有更好的解決方案。

+0

感謝您的建議。 – malaki1974 2012-08-01 14:10:25