1

I hope this makes sense...I have an NSMutableArray that I'm attempting to store multiple UIScrollView's in. Each UIScrollView is going to have multiple images and the ultimate goal is to be able to allow the user to swipe vertically for categories (each instance of UIScrollView) and horizontally for each image in that category (each instance of UIImageView). For now, until I get this code to work, I'm just creating 1 UIScrollView with 1 image and I'm adding that to scrollViewManager. This seems to add everything correctly, but when I leave this function, game over. My Array is empty. I don't understand. Am I supposed to do some sort of deep copy when I add to the Array so it doesn't get destroyed. Perhaps I'll figure it out when I wake up tomorrow, but for now, I'd like to break everything in sight. Thanks, in advance!

EDIT: Sorry for lack of details, I posted this rather late. My project is using ARC, so I'm not releasing scrollView anywhere. Here is the declaration for scrollViewManager in my header file:

@property (nonatomic, retain) NSMutableArray *scrollViewManager;

Here is the code I use to retrieve my scrollView:

UIScrollView *test = (UIScrollView*) [self.scrollViewManager objectAtIndex: 0];

And here is the code to initialize the scrollView and array:

    scrollViewManager = [[NSMutableArray alloc] initWithCapacity: 1];

        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
        [scrollView setBackgroundColor:[UIColor blackColor]];
        [scrollView setCanCancelContentTouches:NO];
        scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
        scrollView.clipsToBounds = YES;     // default is NO, we want to restrict drawing within our scrollview
        scrollView.scrollEnabled = YES;
        scrollView.pagingEnabled = YES;

        NSString *imageName = @"pic1.png";//[NSString stringWithFormat:@"pic%d.jpg", i+1];
        UIImage *image = [UIImage imageNamed:imageName];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

        // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
        CGRect rect = imageView.frame;
        rect.size.height = 400;
        rect.size.width = 300;
        imageView.frame = rect;
        imageView.tag = 1;  // tag our images for later use when we place them in serial fashion
        [scrollView addSubview:imageView];            

        [self.scrollViewManager addObject: scrollView];
Joel
  • 15,654
  • 5
  • 37
  • 60
  • In your first line you have `scrollViewManager`, at the end of your method you have `self.scrollViewManager`. Which one is a typo? – sosborn Feb 09 '13 at 07:24
  • Is scrollViewManager declared as a weak property? – lnafziger Feb 09 '13 at 07:28
  • The code you posted is fine, in and of itself. You need to show more details: what is the scope of `scrollViewManager`. Where and how is it declared? What's the sample code that you call that returns an empty `scrollViewManager` after you execute the code above? – Joel Feb 09 '13 at 07:29
  • @sosborn Technically it doesn't have to be a typo. The first line accesses the class ivar and the last line accesses the getter for that ivar. While not consistent or recommended stylistically, it will work fine as is. – Joel Feb 09 '13 at 07:31
  • Thank you for the comment on my inconsistency. You're right, it's poor/confusing style so I'll fix it! –  Feb 09 '13 at 14:57
  • Again, all the code you posted is fine. You don't need to cast the object to read it, but this should not affect anything. Although if you aren't creating an ivar explicitly you should create it using self.scrollViewManager. – Joel Feb 09 '13 at 19:38
  • Also, I don't know if it's worth mentioning, but the code I posted is in "initWithNibName" and I've attempted to pull the data back from scrollViewManager in the viewWillAppear and viewDidLoad methods, but no luck. –  Feb 09 '13 at 20:05

1 Answers1

0

There are some things that are kind of vague. For example:

  1. Are you using ARC, if not and you are releasing your scrollView anywhere?
  2. Are you using the auto-create-synthesize thing?
  3. What type of property is your scrollViewManager (weak, strong, etc).

If you are creating your ivars automatically when defining a property, your ivar should be called _scrollViewManager instead of scrollViewManager.

The use of self.scrollViewManager is correct when using this method.

I always do this kind of stuff the other way around. When I have a property like this:

@property (nonatomic, strong) NSMutableArray* myArray;

I always initialize them using the self keyword:

self.myArray = [NSMutableArray new];

And when modifying it, I always use the generated ivar:

[_myArray addObject:myFineObject];

I think this is something I kept using when moving from non-ARC to ARC. Not sure if it still the way to go, but it works like expected.

Wim Haanstra
  • 5,918
  • 5
  • 41
  • 57
  • 1. I'm using ARC so I'm not releasing scrollView anywhere. 2. synthesize scrollViewManager; <- this? Yes, this line of code appears just after @implementation. 3. property (nonatomic, retain) NSMutableArray *scrollViewManager; Pardon my ignorance, but I omitted the @ and don't know how to add it in the comment because It's attempting to tag users. –  Feb 09 '13 at 14:52