Array of UIImageView's

Discussion in 'iOS Programming' started by DataBits, Oct 26, 2008.

  1. DataBits macrumors newbie

    Joined:
    Aug 30, 2008
    #1
    Is is possible to create an array of UIImageView's? If so, how is it declared and implemented?

    For example:

    UIImageView *oneView
    UIImageView *twoView
    UIImageView *threeView

    rewritten would result in something like: UIImageView *mynumberView[3]

    Thanks.
     
  2. nacho4d macrumors newbie

    Joined:
    Jul 13, 2008
    #2
    2 ways

    The simplest way is to use arrays like C.
    declare it this way :
    Code:
    UIImageView *imageViewArray[3];
    
    and then call it like this:
    Code:
    imageView[0] = [UIImageView alloc] initWithSomething];
    
    Or if you want to use some classes, then use NSArray or NSMutableArray.

    ie:

    Code:
    NSMutableArray * imagesArray;
    
    imagesArray = [[NSMutableArray alloc] init];
    
    UIImageView *oneImage = [[UIImageView alloc] initWithSomething];
    [imagesArray addObject:oneImage];
    [oneImage release];
    
    UIImageView *Imagetwo = ... (do the same as above)
    
    
    this solved your question?
     
  3. DataBits thread starter macrumors newbie

    Joined:
    Aug 30, 2008
  4. sgiard macrumors newbie

    Joined:
    Jan 28, 2009
    #4
    Newbie question

    Hi everybody.

    I'm new to the iPhone and objective C so I'm sorry if this sounds like a stupid question.

    I'm trying to modify the Touches sample for the iPhone.

    Originally the code is:

    @interface MyView()
    @property (nonatomic, retain) UIImageView *firstPieceView;
    @property (nonatomic, retain) UIImageView *secondPieceView;
    @property (nonatomic, retain) UIImageView *thirdPieceView;
    @property (nonatomic, retain) UILabel *touchPhaseText;
    @property (nonatomic, retain) UILabel *touchInfoText;
    @property (nonatomic, retain) UILabel *touchTrackingText;
    @property (nonatomic, retain) UILabel *touchInstructionsText;
    @property (nonatomic) BOOL piecesOnTop;
    @property (nonatomic) CGPoint startTouchPosition;
    @end

    I want to have an array of 3 (or n) PieceView instead of firstPieceView, secondPieceView and thirdPieceView

    if I put:

    @property (nonatomic, retain) UIImageView *PieceView[3];
    (and change the .h file accordingly)

    I get the error:

    bad property declaration

    What don't I know that causes this?

    Thanx...
     
  5. nacho4d macrumors newbie

    Joined:
    Jul 13, 2008
    #5
    This might help

    , I have tried to do that some time in the past and I remember to have the same error.I think that objective-C compiler don't accept that kind of property declaration. I can't certainly tell.

    As I suggested before, you can always NSArray, (Or NSMutableArray) to have n elements UIImageView.

    Just use
    myArray = [NSMutableArray array]; //if need retain it.
    [myArray addObject:someUIImageView];
    .
    .
    .
    and then retreive it by:

    UIImageView *myImage = [myArray objectAtIndex:i];

    And problem solved! ;)
    Using NSArray class and its subclass NSMutableArray is very easy.

    In this case you probably won't need any UIImageView member variable or property.
    And you probably you could use: (not tested but surely could)
    Code:
    #define firstOne [myArray objectAtIndex:0]
    #define secondOne [myArray objectAtIndex:1] 
    
    Or, What I did in my case, I did realize that there was no need of making PieceView[3] a property (Because at first I tended to make all my variables properties).So did it with out properties.
    Using PieceView for retrieving the i-th image as I suppose you are trying to do.

    Please note that making something a property only is needed when it's going to be called or used from outside that class.
    Good Luck
     
  6. sgiard macrumors newbie

    Joined:
    Jan 28, 2009
    #6
    It works!!!

    Thank you so much for the quick response...

    Everything works great now. :D

    Just a quick follow up question if I may...

    the old code read:

    // Releases necessary resources.
    - (void)dealloc
    {
    // Release each of the subviews
    [firstPieceView release];
    [secondPieceView release];
    [thirdPieceView release];

    ...
    }

    can I just replace this with:

    // Releases necessary resources.
    - (void)dealloc
    {
    // Release each of the subviews
    [myArray release];

    ...
    }

    or do I have to write a loop to release each UIImageView of the array and then release the array?
     
  7. nacho4d macrumors newbie

    Joined:
    Jul 13, 2008
    #7
    you're welcome

    When releasing, it should be enough to release the array. (any loop is not necessary)
    Actually it depends on how you put your objects in the array.
    for example.
    //somewhere inside interface:
    @property (retain) NSArray *arr;
    .
    .
    .
    //inside implementation:

    UIImageView *img1 = [[UIImageView alloc] init];
    //the retain count should be 1,
    //then create an array an set your array property
    NSMutableArray *_arr = [[NSMutableArray alloc] init];
    self.arr = _arr;
    [_arr release];
    [arr addObject:img1];
    //then the retain count of img1 should be 2
    [img1 release];
    //img1 retain count became 1 again.

    //and in your dealloc method:
    [arr release];
    [super dealloc];
    //img1 and arr's retain count should be 0 then everything goes just fine (No leaks)

    A good rule is that you release properties inside dealloc method. And not all the variables you used somewhere else. (because they will be out of scope ;) and should have been released before )


    Maybe this example was not clear enough...
    Try reading some Memory management stuff... I remember I learned a lot from Kochan's book, Programming in Objective-C.
     
  8. sgiard macrumors newbie

    Joined:
    Jan 28, 2009
  9. sanniv macrumors newbie

    Joined:
    Apr 22, 2009

Share This Page