views:

399

answers:

2

I am trying to load an NSMutableArray with UIImageViews. Everything is going fine with that.

Unfortunately, I have no idea how to use the objects when they are in the mutable array.

Here is some code:

UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
NSMutableArray *array = [NSMutableArray new];
[array loadWithObject:(UIImageView *)imageView];
[imageView release];

That kind of sets up what I've done. Here's what I want to do:

[array objectAtIndex:5].center = GCRectMake(0, 0);

but that doesn't work. How can I do this??

+3  A: 

OK, I'll explain the problems you're having. The way to do what you are trying to do is the following:

UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
NSArray *array = [NSArray arrayWithObject:imageView];
[imageView release];
[[array objectAtIndex:0] setCenter:CGPointMake(0.0,0.0)];

First, there is no method -[NSMutableArray loadWithObject:]. Likewise, for your example, you don't really even need a mutable array. Mutable objects have their place, but I usually try to use immutable ones when it makes sense to; as such, I've used NSArray.

Next, you never need to typecast objects when you're adding them to an array. There are a few reasons wherefore your example didn't work:

  1. You were accessing the sixth (starting at one) object in the array. Was there an instance of UIImageView at that index?

  2. For some reason, dot-notation for getters and setters only works when the compiler knows the type of the object you're sending a message to. Since the type of an object that is coming out of an array is not clear at compile-time, you can't use dot-notation. Instead, just use old-fashioned Objective-C method-sending syntax ("brackets and colons").

  3. Finally, it's Core Graphics, not Gore Craphics: hence the prefix is CG, not GC. Also, -[UIImageView setCenter:] takes a CGPoint, not CGRect. So the function you wanted was CGPointMake.

Best of luck to you! Let me know if this helps clear some things up.

Jonathan Sterling
Thanks. Sorry about the GC and RectMake typos. I had those in my code already. I just tried to summarize what I had done. The thing I needed was the setCenter: thing. Works great. Thanks again.
pureman
Okay, new question. How can I get the height/width of the UIImageView frame without using the dot-notation?
pureman
Ooops, here's the confusing part: dot-notation for Objective-C objects only works for clearly typed objects, but dot-notation for normal C structs works just as always. Since `CGRect` is a struct, just try the following: `CGFloat height = [(UIImageView *)[arr objectAtIndex:5] frame].size.height;`.
Jonathan Sterling
worked like magic. thanks again.
pureman
+1  A: 

Hi ! @pureman.

I think you should refer NSMutableArray.

However, I am just giving an overview of NSMutableArray.

  • NSMutableArray = Next Step (NS) Mutable Array
  • Mutable means array can be modified as and when required.
  • Now, This mutable array can hold any kind of objects.
  • Assume that I want to store strings in an array. I would write following statements.

NSMutableArray *anArray=[[NSMutableArray alloc] init];
[anArray addObject:@"Sagar"];
[anArray addObject:@"pureman"];
[anArray addObject:@"Samir"];

  • Here, I found that you need to store imageViews in your requirements.

NSMutableArray *anArray=[[NSMutableArray alloc] init];
UIImageView *imgV1=[[UIImageView alloc] initWithFrame:CGRectMake(10,50,60,70)];
UIImageView *imgV2=[[UIImageView alloc] initWithFrame:CGRectMake(10,110,60,70)];
UIImageView *imgV3=[[UIImageView alloc] initWithFrame:CGRectMake(10,170,60,70)];
UIImageView *imgV4=[[UIImageView alloc] initWithFrame:CGRectMake(10,210,60,70)];
[anArray addObject:imgV1];
[anArray addObject:imgV2];
[anArray addObject:imgV3];
[anArray addObject:imgV4];

  • Now, once ImageViews are added to array, release imageviews as array has it's retain count.

[imgV1 release];
[imgV2 release];
[imgV3 release];
[imgV4 release];

  • Above code will add images to NSMutableArray
  • When you use one of the image from an array, just write down this thing
    UIImageView *x=[anArray objectAtIndex:0];

  • Hope Above descriptions work for you.

  • Add comment, if you don't understood.
sugar
I found this very helpful. Thanks
pureman