3

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
rckoenes
  • 69,092
  • 8
  • 134
  • 166
  • Normally I would expect those actions to live in the controller, not the button. Then you should set the button's actions either in Interface Builder or in your view controller (probably in -loadView); – Thomas Müller Jul 09 '10 at 07:43
  • Well the functions get called, I can see them in the log. But all the changes to the linked imageview won't show. – rckoenes Jul 09 '10 at 08:46
  • Is your UIImageView in view hierarchy? Maybe, you forgot to link it with IB or it's not a subview of any parent view? (this way object will be allocated, but no view visible, of course) – kpower Jul 09 '10 at 09:12
  • I did link the UIImageView, the log shows an allocated imageview. – rckoenes Jul 09 '10 at 09:36

1 Answers1

2

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

Swapnil Luktuke
  • 10,385
  • 2
  • 35
  • 58
  • Thanks it's properly the warm weather that's why I did not see that mistake. Also I do agree that these methods should be in the controller. Except that I need this action so many times and this just saves me allot of time. – rckoenes Jul 09 '10 at 12:53