xcode 目标 c 子类 UIImageView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5539103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
objective c subclass UIImageView
提问by CdB
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:
我正在尝试子类化UIImageView
以在其中添加一些自定义功能。我开始尝试做基础工作,只需使用给定的图像初始化一个对象并将其显示在屏幕上。
使用UIImageView
一切正常,但如果我将对象从UIImageView
自定义类更改为自定义类,则不会显示任何图像。
这是我到目前为止所做的:
//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];
所以,我正在使用这个声明:cell.postImage.image = [UIImage imageNamed:@"img.png"];
在执行之前,我也有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?
如果 postImage 被声明为 UIImageView 一切都按预期工作。但是,如果我将其更改为 Imager,则不会显示任何内容。我错过了什么?
回答by jnic
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.
当您尝试使用点表示法设置图像时image
,您正在合成,从而阻止setImage
对超类(即UIImageView
)的调用。
Remove this line:
删除这一行:
@synthesize image;
回答by drewag
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.
我建议使用Objective-C“类别”而不是子类化UIImageView。只要您不必添加任何成员变量,Category 就是更好的解决方案。当您使用类别时,您可以在原始类的任何实例上调用您的扩展函数(在您的情况下为 UIImageView。这消除了您在任何可能想要使用新函数的地方有意识地使用子类的需要。
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.
然后,无论你想在 UIImageView 上使用你的新函数,你只需要包含头文件。