Custom UIButton cant interact with linked UIImageView

蹲街弑〆低调 提交于 2019-12-07 15:52:14

问题


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

回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!