I'm stuck on the following code. Some how my UIButton Extended class cant show or hide an UIImageView
My methods are being called and the imageview is not nil.
Here is the code:
@interface UILinkedImageButton : UIButton {
IBOutlet UIImageView *linkImageView;
}
@property (nonatomic, retain) IBOutlet UIImageView *linkImageView;
@end
#import "UILinkedImageButton.h"
@interface UILinkedImageButton ()
- (void)showImage;
- (void)hideImage;
@end
-------------------------------------------------------------------------------------------------
@implementation UILinkedImageButton
@synthesize linkImageView;
- (void) dealloc{
[linkImageView release], linkImageView = nil;
[super dealloc];
}
- (id) initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self){
[self addTarget:self action:@selector(showImage) forControlEvents:UIControlEventTouchDown];
[self addTarget:self action:@selector(hideImage) forControlEvents:UIControlEventTouchUpInside];
[self addTarget:self action:@selector(hideImage) forControlEvents:UIControlEventTouchUpOutside];
}
return self;
}
- (void)showImage
{
if(self.imageView){
NSLog(@"UILinkImageButton - showImage - currentStatus: %@", self.imageView);
self.imageView.hidden = NO;
[self.superview layoutIfNeeded];
}
}
- (void)hideImage
{
if(self.imageView){
NSLog(@"UILinkImageButton - hideImage");
self.imageView.hidden = YES;
}
}
@end
As Thomas Müller mentions in the comment i too think the actions should be in the controller.
Apart from that, in your code you are changing the hidden property of 'imageView' object while the custom image view you have created in you declaration is 'linkImageView'. The code is not throwing an error because 'imageView' is the button's readonly property declared in UIButton and it represents the button image view not your linkImageView.
Hope this helps.
Thanks, Swapnil
来源:https://stackoverflow.com/questions/3210649/custom-uibutton-cant-interact-with-linked-uiimageview