I built a UIButton subclass that is a very basic checkbox. Everything is working (e.g. I'm getting the events I expect, the drawRect method is called when I expect, etc), but my image isn't drawing. I've checked that my image is not nil and that my images are different.
Code:
-(void)awakeFromNib {
[super awakeFromNib];
[self addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
self.state=YES;
}
-(void) buttonClick {
self.state = !self.state;
[self setNeedsDisplay];
}
-(void)drawRect:(CGRect)rect {
UIImage *img = nil;
if (self.state) {
img = [self backgroundImageForState:UIControlStateSelected];
}
else {
img = [self backgroundImageForState:UIControlStateNormal];
}
[img drawInRect:rect];
}
Checking the value of 'img' before it is drawn:
Printing description of img:
<UIImage: 0x52a710>
(gdb) continue
Printing description of img:
<UIImage: 0x52a5d0>
The background images are being set in the NIB. If I change the selected state in IB, I see the proper image show up. I'm also getting a valid image back from the call (which I don't if I try to get imageForState, which is not set in the NIB).
The reason for the subclass is that I need a checkbox. UIButton doesn't do that and UISwitch is hideously ugly for all but the most utilitarian views.
Thoughts?
Thanks.
tj
Edit: Added all the code for the subclass