Reputation: 4908
I'm trying to subclass UIImageView
to add some custom functionality in it. I started trying to do the basics, just init an object with a given image and display it on the screen.
Using UIImageView
everything works fine, but if i change my object from UIImageView
to my custom class, no image is displayed.
Here's what i've done so far:
//Imager.h
#import <UIKit/UIKit.h>
@interface Imager : UIImageView {
}
-(id) init;
-(void) retrieveImageFromUrl: (NSString *)the_URL;
@end
//Imager.m
#import "Imager.h"
@implementation Imager
@synthesize image;
-(id)init
{
self = [super init];
return self;
}
-(void) retrieveImageFromUrl: (NSString *)the_URL{
//Not implemented yet
}
@end
So, i am using this statment: cell.postImage.image = [UIImage imageNamed:@"img.png"];
Before this is executed, i also have postImage = [[UIImageView alloc] init];
If postImage is declared as UIImageView everything works as expected. But if i change it to Imager instead, then nothing is displayed. What am i missing?
Upvotes: 1
Views: 6084
Reputation: 94723
I would suggest using an Objective-C "Category" instead of subclassing the UIImageView. As long as you don't have to add any member variables, a Category is a better solution. When you use a category you can call your extended functions on any instance of the original class (in your case UIImageView. That removes the need for you to consciously use your subclass anywhere you might want to use your new functions.
You can just do the following in a header:
@interface UIImageView (UIImageView+URL)
-(void) retrieveImageFromUrl: (NSString *)the_URL;
@end
Then in an implimentation file:
@implimentation UIImageView (UIImageView+URL)
-(void) retrieveImageFromUrl: (NSString *)the_URL
{
// implimentation
}
@end
Then wherever you want to use your new function on a UIImageView, you just need to include the header file.
Upvotes: 3
Reputation: 8785
You're synthesizing image
, thus blocking calls to setImage
on the superclass (i.e. UIImageView
) when you attempt to use the dot notation to set the image.
Remove this line:
@synthesize image;
Upvotes: 4