我有一个自定义类A,它是UIView
的子类,它基本上管理一个符合视图大小的UIScrollView
,并包含一个填充该scrollView的UIImageView
。 / p>
我无法设置自定义类的imageView.image:
这是班级
#import "BackgroundPickerView.h"
@implementation BackgroundPickerView
@synthesize scrollView;
@synthesize imageView;
@synthesize actionButton;
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
return self;
}
-(void)layoutSubviews
{
[super layoutSubviews];
//[[NSNotificationCenter defaultCenter] addObserver:self
// selector:@selector(changeImage)
// name:@"ImageChangeNotification"
// object:nil];
//Here's where we add custom subviews
scrollView = [[UIScrollView alloc] init];
imageView = [[UIImageView alloc] init];
actionButton = [[UIButton alloc] init];
[scrollView setFrame:CGRectMake(0, 0, 321, 115)];
[imageView setFrame:CGRectMake(0, 0, 321, 115)];
[actionButton setFrame:CGRectMake(0, 0, 320, 115)];
scrollView.scrollEnabled = YES;
scrollView.minimumZoomScale = 1.0;
scrollView.maximumZoomScale = 6.0;
scrollView.contentSize = CGSizeMake(300, 200);//imageView.image.size;
[scrollView addSubview:imageView];
[self addSubview:scrollView];
}
-(void)storeImage:(UIImage *)image
{
self.imageView.image = image;
}
-(void)changeImage
{
[self.imageView setImage:[UIImage imageNamed:@"random.png"]];
}
从我接收图像的其他类B我尝试使用NSNotification和一个简单的setter方法尝试设置类对象的 imageView 属性,但似乎无法设置imageView.image 。我尝试过使用
instance.imageView.image = myImageToSet
//Setter
[instance storeImage:myImageToSet];
在B级,但是没有成功
答案 0 :(得分:1)
首先,layoutSubviews
中子视图的初始化不是最好的方式,更好的是在init
方法中初始化它们,因为layoutSubviews
可能多次调用来布局子视图,如果您将初始化代码放在layoutSubviews
中,则可能存在子视图。在layoutSubviews
中,您只需设置子视图的frame
。
你可以这样做,
#import "BackgroundPickerView.h"
@implementation CustomViewA
//Edit change below
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
[self commonInit];
}
return self;
}
- (void)awakeFromNib
{
[self commonInit];
}
- (void)commonInit
{
_scrollView = [[UIScrollView alloc] init];
_imageView = [[UIImageView alloc] init];
_actionButton = [[UIButton alloc] init];
_scrollView.scrollEnabled = YES;
_scrollView.minimumZoomScale = 1.0;
_scrollView.maximumZoomScale = 6.0;
[_scrollView addSubview:_imageView];
[self addSubview:_scrollView];
}
- (void)layoutSubviews
{
[super layoutSubviews];
[_scrollView setFrame:CGRectMake(0, 0, 321, 115)];
[_imageView setFrame:CGRectMake(0, 0, 321, 115)];
[_actionButton setFrame:CGRectMake(0, 0, 320, 115)];
_scrollView.contentSize = CGSizeMake(300, 200);
}
-(void)storeImage:(UIImage *)image
{
self.imageView.image = image;
}
-(void)changeImage
{
[self.imageView setImage:[UIImage imageNamed:@"random.png"]];
}
synthesise
是可选的
在BackgroundPickerView.h
文件中,它就像下面的
@interface BackgroundPickerView : UIView
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIButton *actionButton;
-(void)storeImage:(UIImage *)image;
-(void)changeImage;
对于图像你检查它应该工作,检查图像是否存在,
修改强>
对于图像,例如在controller
中
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_viewA = [[BackgroundPickerView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:_viewA];
}
//for testing
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[_viewA storeImage:[UIImage imageNamed:@"6.png"]]; //hear do this
}