Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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..

share|improve this question
    
Where do you have your many image views? How are they defined? –  Wain Jul 20 '13 at 22:21

4 Answers 4

up vote 1 down vote accepted

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.

share|improve this answer
    
thank u.. where would i release the array?? –  nouf Jul 20 '13 at 23:28
    
imagesArray[0] = l1 is legal too, more java-esque. –  Chris Wagner Jul 21 '13 at 4:46
1  
Just use ARC @nouf, unless of course there is a really good reason you can't... In that case [imagesArray release] when you no longer need it. and [l1 release] and [l2 release] after you add them to the array. –  Chris Wagner Jul 21 '13 at 4:48
    
@nouf : I have edited the answer for you. Check it now. –  Jamal Zafar Jul 21 '13 at 5:16

Using the more modern syntax you can say

NSArray *myViewArray=@[ view1, view2, view3 ];
share|improve this answer
UILabel * l1 = [[UILabel alloc] init];
UILabel * l2 = [[UILabel alloc] init];
NSMutableArray * arrayOfLabels = [NSMutableArray arrayWithCapacity:2];
arrayOfLabels[0] = l1;
arrayOfLabels[1] = l2;
share|improve this answer
UIImageView *view1;
UIImageView *view2;
// assuming they are already instantiated
NSMutableArray *arrayOfImageViews = [[NSMutableArray alloc] init];
[arrayOfImageViews addObject:view1];
[arrayOfImageViews addObject:view2];
share|improve this answer
    
So view1 is at index 0 and view2 at index 1 right??.. what if then i wanted to change the element at index 1 to view1 ?? i mean how can i access a specific element for modifying?? –  nouf Jul 20 '13 at 22:54
    
That would be [arrayOfImageViews replaceObjectAtIndex:1 with Object:view1];. Note that the array contains pointers to real objects (e.g. view1, view2). You can't modify a pointer, only replace. However you can modify the element the pointer points to. You can access it with [arrayOfImageViews objectAtIndex:1] –  Thomas Leu Jul 20 '13 at 23:57
    
thank you Thomas :) –  nouf Jul 21 '13 at 0:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.