Chisx Chisx - 6 months ago 25
Objective-C Question

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.

I am having trouble setting the imageView.image of the custom class:

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. I have tried using

instance.imageView.image = myImageToSet


//Setter
[instance storeImage:myImageToSet];


in class B but am having no success

Answer

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. in layoutSubviews u only set the frame of the subviews.

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

 @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,

- (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
 }