scrollview无法显示内容
我是创建一个scrollview,然后在里面添加imageview,imageView的创建主要代码如下:
@inte易做图ce ImagePageView : UIView
@property (nonatomic,retain)UIImageView *imageViewOne;
@property (nonatomic,retain)UIImageView *imageViewTwo;
- (void)imageViewV:(int)pageNum;
- (void)imageViewH:(int)pageNum;
@implementation ImagePageView
@synthesize imageViewOne = _imageViewOne;
@synthesize imageViewTwo = _imageViewTwo;
- (UIImageView *)imageViewOne
{
if (!_imageViewOne) {
_imageViewOne = [[UIImageView alloc] init];
[self addSubview:_imageViewOne];
}
return _imageViewOne;
}
- (UIImageView *)imageViewTwo
{
if (!_imageViewTwo) {
_imageViewTwo = [[UIImageView alloc] init];
[self addSubview:_imageViewTwo];
}
return _imageViewTwo;
}
- (void)imageViewV:(int)pageNum
{
_imageViewOne.image = [[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[array objectAtIndex:pageNum] ofType:nil]] autorelease];
_imageViewOne.frame = self.bounds;
}
就是这样的代码,结果不能显示。不久找到了问题,红色部分应该改为:
self.imageViewOne.image = [[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[array objectAtIndex:pageNum] ofType:nil]] autorelease];
self.imageViewOne.frame = self.bounds;
原因是这样的,红色代码并不会调用初始化的代码(- (UIImageView *)imageViewTwo/- (UIImageView *)imageViewOne),所以imageViewOne为nil,导致无法显示图片。
而self.imageViewOne则会调用get方法。
补充:移动开发 , IOS ,