nouf
nouf

Reputation: 31

An array of UIImageViews in objective-c

I already had many UIImageViews.. I want to make an array of UIImageViews and assign every element of the array with one of the UIImageViews above.. How can I do that with objective c?? I did it in java like the following:

JLabel l1=new JLabel();
JLabel l2=new JLabel();
JLabel [] arrayOfLabels = new JLabel[2];
arrayOfLabel[0] = l1;
arrayOfLabel[1] = l2;

I need to do the same thing in objective c..

Upvotes: 0

Views: 111

Answers (4)

Jamal Zafar
Jamal Zafar

Reputation: 2189

Let me answer you according to your Java Statements for better clarity:

   //Java:
    JLabel l1=new JLabel();

    //Objective C:
    UIImageView * l1= [[UIImageView alloc] init];


    //Java:
    JLabel l2=new JLabel();

    //Objective C:
    UIImageView * l2 = [[UIImageView alloc] init];


    //Java 
    JLabel [] arrayOfLabels = new JLabel[2]; 

    //Objective C 
    NSMutableArray * imagesArray = [[NSMutableArray alloc] init];

    //Java 
    arrayOfLabel[0] = l1;

    //Objective C 
    [imagesArray addObject:l1];


    //Java
    arrayOfLabel[1] = l2; 

    //Objective C
    [imagesArray addObject:l2];

Since you are not using ARC (i guessed it from your comment), so therefore you must release the things manually as part of memory management as:

     [l1 release];  //After adding it to imagesArray

     [l2 release];  //After adding it to imagesArray

And release the imagesArray when you don't need it. Normally it is done in dealloc(), but you can release it at any point where you don't need it further by simply calling:

    [imagesArray release];
    imagesArray = nil;

Hope so this will help you.

Upvotes: 1

Gerd K
Gerd K

Reputation: 2993

Using the more modern syntax you can say

NSArray *myViewArray=@[ view1, view2, view3 ];

Upvotes: 0

Blazej SLEBODA
Blazej SLEBODA

Reputation: 9915

UILabel * l1 = [[UILabel alloc] init];
UILabel * l2 = [[UILabel alloc] init];
NSMutableArray * arrayOfLabels = [NSMutableArray arrayWithCapacity:2];
arrayOfLabels[0] = l1;
arrayOfLabels[1] = l2;

Upvotes: 1

Thomas Leu
Thomas Leu

Reputation: 845

UIImageView *view1;
UIImageView *view2;
// assuming they are already instantiated
NSMutableArray *arrayOfImageViews = [[NSMutableArray alloc] init];
[arrayOfImageViews addObject:view1];
[arrayOfImageViews addObject:view2];

Upvotes: 0

Related Questions