I have subclassed UIButton to draw graphics, but the “addTarget” won’t work.
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
I did not do anything with making adjustments to that method.
The Button graphics work ok on the “touches”.
If I use a standard UIButton then the “addTarget” works fine.
Not sure what I am missing??
thx
#import <UIKit/UIKit.h>
@interface CButton : UIButton
{
CGContextRef c;
int myState;
}
@end
#import "CButton.h"
@implementation CButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
//NSLog(@"init state: %d", self.state);
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
// myState = 1;
myState = !myState;
// NSLog(@"BeginState: %d", self.state);
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
// myState = 0;
// NSLog(@"EndState: %d", self.state);
// [self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
@end
You have to call super from your event handlers when you’re all done handling events. How else will the button know of the touches to call your action?
e.g.
That being said, the comments on your original post are right, subclassing UIButton can get tricky. But it looks as if you have things under control (mostly). Good luck!