ios - Trouble setting UIImageView.image on custom class from another class -


i have custom class subclass of uiview manages uiscrollview fits size of view , contains uiimageview fills scrollview.

i having trouble setting imageview.image of custom class:

here's 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 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 other class b receive image have tried using nsnotification , simple setter method try setting imageview property of class object, cannot seem set imageview.image. have tried using

instance.imageview.image = myimagetoset

//setter [instance storeimage:myimagetoset]; 

in class b having no success

first of all, initialisation of subviews in layoutsubviews not best way, better u initialise them in init method, because layoutsubviews may call multiple times layout subviews, if u place initialisation code in layoutsubviews there might may subviews. in layoutsubviews u set frame of subviews.

and u can 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 optional , in backgroundpickerview.h file 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 should work, check image present or not,

edit image, in controller example,

- (void)viewdidload {   [super viewdidload];    // additional setup after loading view, typically 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  } 

Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -