2012-01-20 218 views
0

我想繼承自定義UIButton繼承自定義的UIButton

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 

我該怎麼辦呢?我該怎麼辦:

[self performSelectorOnMainThread:@selector(setBackgroundImage:forState:) withObject:image waitUntilDone:YES]; 

這是可能的嗎?

+0

試圖子類或使用「UIButtonTypeCustom」? –

+0

你不應該繼承'UIButton'。請參閱http://stackoverflow.com/questions/5045672/create-uibutton-subclass。 – smparkes

+0

嗯..試圖創建一個類,lazily加載backgroundImage,那麼我該怎麼做呢? – adit

回答

1

只寫你自己的子類是這樣的: MyCustomButton.h文件:

#import <Foundation/Foundation.h> 

@interface MyCustomButton : UIButton 
    // Declare custom properties, etc 
@end 

MyCustomButton.m文件:

#import "MyCustomButton.h" 

@implementation MyCustomButton 
    // @synthesize Custom Properties 
    // Add in custom views, methods, etc 
@end 

然後在你的其他類,是指它作爲一個MyCustomButton代替UIButton

爲後臺加載背景圖片(或任何圖片)使用塊。