0
votes

I have a class called packages that creates a uiscrollview.

I have a getter for this class and once called, it returns a UIScroll view.

I would like my main application to set its scroll view to the one returned by the app. here is code that returns the scroll view:

MyClass.m

-(UIScrollView *)makeScrollView{

    CGFloat hue = ( arc4random() % 256 / 256.0 );  //  0.0 to 1.0
    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from white
    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from 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 i = 0;
    for(NSString *item in self.pictureArray){

        UIImageView *imageView1;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
        tap.delegate = self;

        xOrigin = i * 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];
        i += 1;
    }

    // Set the contentSize equal to the size of the UIImageView
    // scrollView.contentSize = imageView.scrollview.size;
    self.scrollView.contentSize = CGSizeMake(numberOfViews * imageSize, 70);
    return self.scrollView;
}

and its being set using a uipickerview selection inside of my ViewController:

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
      inComponent:(NSInteger)component
{
    NSLog(@"Changing the scroll view");
    self.scrollView = [self.packages[(int)row] makeScrollView];
}

I know the picker view is being called because I am getting the proper logging message. However, my ScrollView inside of my ViewController is not being set.

enter image description here

The above view, which I can access with self.scrollView is the view I need to change.

3
are u using the view as shared view, any time u are calling to makeScrollView method returns the scroll view to your controller ... this is asking becz u are doing similar to this i am not sure, if i get this question correctlyShankar BS
Yes, I want to use that UIScrollView as a shared view. A place holder for any makeScrollView that are called.Cripto

3 Answers

1
votes

You have assigned the scroll view to a property, but you need to add that scrollview to view via [self.view addSubView:self.scrollView];

1
votes

You have create a new scrollView and you haven't added to interface.And you should remove previous view if need.

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
      inComponent:(NSInteger)component
{
    NSLog(@"Changing the scroll view");
    if(self.scrollView){
        [self.scrollView removeFromSuperView];
    }

    self.scrollView = [self.packages[(int)row] makeScrollView];
    [self.view addSubview:self.scrollView];
}
1
votes

i don't no what u are doing with the picker view, but as u said using the scroll view as shared view u can do like below

i assume your class named MyClass is a shared class, this class provides a scrollview that is created once,

in MyClass.h file


  #import <Foundation/Foundation.h>

  @interface MyClass : NSObject //it is subclass of the NSObject
  @property (nonatomic, retain) UIScrollView *MySharedScrollVIew; //say u hav a shared scroll view
  @property (nonatomic, retain) NSArray *pictureArray;

  + (id)sharedManager;
  //you dont need to make scroll view becz it is alreday create u can access it with 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 i = 0;
   for(NSString *item in _pictureArray){

    UIImageView *imageView1;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
 //   tap.delegate = self;

    xOrigin = i * 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];
    i += 1;
  }

  // Set the contentSize equal to the size of the UIImageView
  // scrollView.contentSize = imageView.scrollview.size;
  _MySharedScrollVIew.contentSize = CGSizeMake(numberOfViews * imageSize , 70);

 }

  - (void)handleTapFrom:(id)sender
  {
     NSLog(@"Tapped");
  }

 @end


in the controller u can just access that shared scrollview like below import the class header #import "MyClass.h"

- (void)viewDidLoad
 {
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    MyClass *sharedObj = [MyClass sharedManager];//get the shared class object
    UIScrollView *scrollView = sharedObj.MySharedScrollVIew;
    scrollView.frame = CGRectMake(0 ,20,320 , 300);//set the frame for scroll view hear
    scrollView.backgroundColor = [UIColor greenColor];
   [self.view addSubview:scrollView];

 }