0

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

1
  • Where do you have your many image views? How are they defined? Commented Jul 20, 2013 at 22:21

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

imagesArray[0] = l1 is legal too, more java-esque.
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.
1
UILabel * l1 = [[UILabel alloc] init];
UILabel * l2 = [[UILabel alloc] init];
NSMutableArray * arrayOfLabels = [NSMutableArray arrayWithCapacity:2];
arrayOfLabels[0] = l1;
arrayOfLabels[1] = l2;

Comments

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

2 Comments

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??
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]
0

Using the more modern syntax you can say

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.