ios - Change UIScrollView reference programmatically -
i have class called packages creates uiscrollview.
i have getter class , once called, returns uiscroll view.
i main application set scroll view 1 returned app. here code returns scroll view:
myclass.m
-(uiscrollview *)makescrollview{ cgfloat hue = ( arc4random() % 256 / 256.0 ); // 0.0 1.0 cgfloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 1.0, away white cgfloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 1.0, away black uicolor *color = [uicolor colorwithhue:hue saturation:saturation brightness:brightness alpha:1]; [self.scrollview setbackgroundcolor:color]; [self.scrollview setindicatorstyle:uiscrollviewindicatorstylewhite]; [self.scrollview setpagingenabled : no]; [self.scrollview setcancancelcontenttouches:yes]; [self.scrollview setuserinteractionenabled:yes]; self.scrollview.scrollenabled = true; self.scrollview.bounces = true; self.scrollview.contentsize = cgsizemake([uiscreen mainscreen].bounds.size.width, [uiscreen mainscreen].bounds.size.height); self.scrollview.userinteractionenabled = yes; cgfloat xorigin = 0; int numberofviews = [self.picturearray count]; // uiimageview *imageview1; int = 0; for(nsstring *item in self.picturearray){ uiimageview *imageview1; uitapgesturerecognizer *tap = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(handletapfrom:)]; tap.delegate = self; xorigin = * imagesize; imageview1 = [[uiimageview alloc] initwithframe:cgrectmake(xorigin,50,100,50)]; imageview1.tag = i; [imageview1 setuserinteractionenabled:yes]; [imageview1 addgesturerecognizer:tap]; [imageview1 setimage:[uiimage imagenamed:item]]; [self.scrollview addsubview:imageview1]; += 1; } // set contentsize equal size of uiimageview // scrollview.contentsize = imageview.scrollview.size; self.scrollview.contentsize = cgsizemake(numberofviews * imagesize, 70); return self.scrollview; } and being set using uipickerview selection inside of viewcontroller:
-(void)pickerview:(uipickerview *)pickerview didselectrow:(nsinteger)row incomponent:(nsinteger)component { nslog(@"changing scroll view"); self.scrollview = [self.packages[(int)row] makescrollview]; } i know picker view being called because getting proper logging message. however, scrollview inside of viewcontroller not being set.

the above view, can access self.scrollview view need change.
i don't no u doing picker view, u said using scroll view shared view u can below
i assume class named myclass shared class, class provides scrollview created once,
in myclass.h file
#import <foundation/foundation.h> @interface myclass : nsobject //it subclass of nsobject @property (nonatomic, retain) uiscrollview *mysharedscrollview; //say u hav shared scroll view @property (nonatomic, retain) nsarray *picturearray; + (id)sharedmanager; //you dont need make scroll view becz alreday create u can access shared class object @end in myclass.m file
#import "myclass.h" static myclass *sharedmymanager = nil; static dispatch_once_t oncetoken; @implementation myclass @synthesize mysharedscrollview = _mysharedscrollview; @synthesize picturearray = _picturearray; + (id)sharedmanager { dispatch_once(&oncetoken, ^{ sharedmymanager = [[self alloc] init]; }); return sharedmymanager; } - (id)init { if (self = [super init]) { _picturearray = [[nsarray alloc]initwithobjects:@"22.jpg",@"32.jpg",@"33.jpg", nil]; _mysharedscrollview = [[uiscrollview alloc]init]; [self initilisethescrollview]; } return self; } - (void)initilisethescrollview { int imagesize = 100;// _mysharedscrollview.contentsize = cgsizemake(500, 600); _mysharedscrollview.scrollenabled = true; _mysharedscrollview.bounces = true; _mysharedscrollview.userinteractionenabled = yes; cgfloat xorigin = 0; int numberofviews = [_picturearray count]; // uiimageview *imageview1; int = 0; for(nsstring *item in _picturearray){ uiimageview *imageview1; uitapgesturerecognizer *tap = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(handletapfrom:)]; // tap.delegate = self; xorigin = * imagesize; imageview1 = [[uiimageview alloc] initwithframe:cgrectmake(xorigin,50,100,50)]; imageview1.tag = i; [imageview1 setuserinteractionenabled:yes]; [imageview1 addgesturerecognizer:tap]; [imageview1 setimage:[uiimage imagenamed:item]]; [_mysharedscrollview addsubview:imageview1]; += 1; } // set contentsize equal size of uiimageview // scrollview.contentsize = imageview.scrollview.size; _mysharedscrollview.contentsize = cgsizemake(numberofviews * imagesize , 70); } - (void)handletapfrom:(id)sender { nslog(@"tapped"); } @end in controller u can access shared scrollview below import class header #import "myclass.h"
- (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. myclass *sharedobj = [myclass sharedmanager];//get shared class object uiscrollview *scrollview = sharedobj.mysharedscrollview; scrollview.frame = cgrectmake(0 ,20,320 , 300);//set frame scroll view hear scrollview.backgroundcolor = [uicolor greencolor]; [self.view addsubview:scrollview]; }
Comments
Post a Comment