0

I am trying to subclass a custom UIButton:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

How do I do that? Also how do I do:

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

is this possible?

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
adit
  • 32,574
  • 72
  • 229
  • 373
  • trying to subclass or use the "UIButtonTypeCustom"? – Jason Bugs Adams Jan 20 '12 at 22:18
  • You're not supposed to subclass `UIButton`. See http://stackoverflow.com/questions/5045672/create-uibutton-subclass. – smparkes Jan 20 '12 at 22:27
  • hmm.. trying to create a class that lazily loads the backgroundImage, so how do I do this then? – adit Jan 20 '12 at 22:30
  • Use composition instead of inheritance: create a custom view class with the button as a subview. – smparkes Jan 21 '12 at 02:43
  • Using `performSelectorOnMainThread:withObject:waitUntilDone:` is completely useless in your case. Why using this kind of method instead of calling the method directly, if you are already on the main thread? (And if you are not, use GCD instead, by the way). This does not lazy-load the bkgImage, by the way. Also your question is not clear do you intend to subclass or use custom type? And what's the link between your subclassing question and the setBackgroundImage call? – AliSoftware Oct 13 '12 at 15:26

1 Answers1

1

Just write your own subclass like this: MyCustomButton.h file:

#import <Foundation/Foundation.h>

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

MyCustomButton.m file:

#import "MyCustomButton.h"

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

Then in your other classes, refer to it as a MyCustomButton instead of UIButton

For the background loading of background image (or any image) use blocks.

LJ Wilson
  • 14,445
  • 5
  • 38
  • 62