[英]Trouble setting UIImageView.image on custom class from another class
I have a custom class A that is a subclass of UIView
which essentially manages a UIScrollView
that fits the size of the view and the contains an UIImageView
that fills that scrollView. 我有一个自定义类A,它是
UIView
的子类,该类实质上管理一个适合视图大小的UIScrollView
,并且包含一个填充该scrollView的UIImageView
。
I am having trouble setting the imageView.image of the custom class: 我在设置自定义类的imageView.image时遇到麻烦:
Here's the class 这是课程
#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"]];
}
From my other class B where I receive the image I have tried using NSNotification and a simple setter method to try setting the imageView property of the class object, but cannot seem to set the imageView.image. 在我收到图像的其他B类中,我尝试使用NSNotification和一个简单的setter方法尝试设置该类对象的imageView属性,但似乎无法设置imageView.image。 I have tried using
我尝试使用
instance.imageView.image = myImageToSet
//Setter
[instance storeImage:myImageToSet];
in class B but am having no success B班,但没有成功
first of all, the initialisation of subviews in layoutSubviews
not the best way, better u initialise them in the init
method, because layoutSubviews
may call multiple times to layout the subviews, if u place the initialisation code in the layoutSubviews
there might be may subviews. 首先,在
layoutSubviews
初始化子视图不是最好的方法,最好使用init
方法对其进行init
,因为layoutSubviews
可能会多次调用以布局子视图,如果您将初始化代码放置在layoutSubviews
则可能会有子视图。 in layoutSubviews
u only set the frame
of the subviews. 在
layoutSubviews
只能设置子视图的frame
。
and u can do like below, 你可以像下面这样
#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
is optional and in BackgroundPickerView.h
file it is something like below 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;
for image u check it it should work, check the image is present or not, 对于图片,请检查它是否可以正常工作,检查图片是否存在,
Edit For the image, in controller
for example, 编辑对于图像,例如在
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.