Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-09-2011, 02:49 PM   #1 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 31
sgr_81 is on a distinguished road
Default UIImageView dynamically to array

Hello All,

Im very new on this, I have been locking for 2 days if it is possible to do this:

in my .h file I have:
@interface puzzle2ViewController : UIViewController {
IBOutlet UIImageView *pza1;
IBOutlet UIImageView *pza2;
.....
}
@property(assign) IBOutlet UIImageView *pza1;
@property(assign) IBOutlet UIImageView *pza2;

in my .m file I have:
#define kNumeroDePiezas 2
piezas = [[NSMutableArray alloc] initWithCapacity:kNumeroDePiezas];
for(int i = 0; i < kNumeroDePiezas; i++) {
UIImageView *myView = [NSString stringWithFormat:@"pza%d", i];
[piezas addObject:myView];
}

As you can see I want to add the UIImageView dynamically to a NSMutableArray. Why? because I whant it dynamic, now I have 2 images but probably I will have 10 or 20 images.

The images are in the interface builder with reference outlets already seted up.

Hope somebody can help me, thanks.
sgr_81 is offline   Reply With Quote
Old 04-09-2011, 03:05 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by sgr_81 View Post
Hello All,

Im very new on this, I have been locking for 2 days if it is possible to do this:

in my .h file I have:
@interface puzzle2ViewController : UIViewController {
IBOutlet UIImageView *pza1;
IBOutlet UIImageView *pza2;
.....
}
@property(assign) IBOutlet UIImageView *pza1;
@property(assign) IBOutlet UIImageView *pza2;

in my .m file I have:
#define kNumeroDePiezas 2
piezas = [[NSMutableArray alloc] initWithCapacity:kNumeroDePiezas];
for(int i = 0; i < kNumeroDePiezas; i++) {
UIImageView *myView = [NSString stringWithFormat:@"pza%d", i];
[piezas addObject:myView];
}

As you can see I want to add the UIImageView dynamically to a NSMutableArray. Why? because I whant it dynamic, now I have 2 images but probably I will have 10 or 20 images.

The images are in the interface builder with reference outlets already seted up.

Hope somebody can help me, thanks.

Yes, you can certainly store image views into an array. An NSArray will hold ANY object.

However, your code is creating strings, and saving those to an array.

This line:

Code:
UIImageView *myView = [NSString stringWithFormat:@"pza%d", i];
Should generate a compiler warning, because you're assigning a string to a variable of type UIImageView.

You probably meant to create your image views from images that you load with imageNamed or imageWithContentsOfFile.
Something like this:

Code:
NSString* docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString* filename = [NSString stringWithFormat:@"pza%d", i];
NSString* pathName = [docsDir stringByAppendingPathComponent: filename];
UIImage* anImage = [UIImage imageWithContentsOfFile: pathName];
UIImageView* anImageView = [[[UIImageView alloc] initWithImage: anImage] autorelease];

[imageViewArray addObject: anImageView];
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 04-09-2011, 03:21 PM   #3 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 31
sgr_81 is on a distinguished road
Default

wow!, how fast you are Duncan, thanks so much,

just one more question, I have already tried your solution, does it will work if i have this:


The images are placed in the interface builder and referenced.

i changed my code accordingly:
piezas = [[NSMutableArray alloc] initWithCapacity:kNumeroDePiezas];
for(int i = 0; i < kNumeroDePiezas; i++) {
NSString* docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDire ctory, NSUserDomainMask, YES) lastObject];
NSString* filename = [NSString stringWithFormat:@"pza%d", i];
NSString* pathName = [docsDir stringByAppendingPathComponent: filename];
UIImage* anImage = [UIImage imageWithContentsOfFile: pathName];
UIImageView* anImageView = [[[UIImageView alloc] initWithImage: anImage] autorelease];

[piezas addObject: anImageView];

NSLog(@"the array is %f",piezas);

And I get in the console:

2011-04-09 15:16:54.810 puzzle2[14422:207] the array is 0.000000
2011-04-09 15:16:54.812 puzzle2[14422:207] the array is 0.000000

Thanks

Last edited by sgr_81; 04-09-2011 at 03:33 PM. Reason: image url error, typo error
sgr_81 is offline   Reply With Quote
Old 04-09-2011, 03:48 PM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by sgr_81 View Post
wow!, how fast you are Duncan, thanks so much,

just one more question, I have already tried your solution, does it will work if i have this:


The images are placed in the interface builder and referenced.

i changed my code accordingly:
piezas = [[NSMutableArray alloc] initWithCapacity:kNumeroDePiezas];
for(int i = 0; i < kNumeroDePiezas; i++) {
NSString* docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDire ctory, NSUserDomainMask, YES) lastObject];
NSString* filename = [NSString stringWithFormat:@"pza%d", i];
NSString* pathName = [docsDir stringByAppendingPathComponent: filename];
UIImage* anImage = [UIImage imageWithContentsOfFile: pathName];
UIImageView* anImageView = [[[UIImageView alloc] initWithImage: anImage] autorelease];

[piezas addObject: anImageView];

NSLog(@"the array is %f",piezas);

And I get in the console:

2011-04-09 15:16:54.810 puzzle2[14422:207] the array is 0.000000
2011-04-09 15:16:54.812 puzzle2[14422:207] the array is 0.000000

Thanks


I don't understand what you're trying to do. Are you saying that you have a bunch of image views that you defined in Interface builder and you want to put THOSE into an array?

In that case, I would suggest using tags. There is a UIView method viewWithTag that finds a subview with a particular numeric tag. You could set up your image views in IB with tags that start at 1 and go up from there, and then use code like this:


Code:
UIImageView* anImageView;
for(int i = 0; i < kNumeroDePiezas; i++) 
{
  anImageView = [[self view] viewWithTag: i+1];
  if (anImageView)
    [piezas addObject: anImageView];
}
NSLog(@"the piazzas array contains %d items. Contents = %@",[piazzas count], piazzas);
Note that you should use the "%@" format string to display an array (or any other object.) The "%f" format string you used in your log statement is for displaying a floating point value, and you will get odd results if you try to log an object with that format.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 04-09-2011, 05:33 PM   #5 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 31
sgr_81 is on a distinguished road
Default

Thats exactly what I was looking for!!!! I read something about tags but I did not put atention, that is why I will never found a solution. thanks so much for your help so I can continue with my little project.
sgr_81 is offline   Reply With Quote
Old 04-09-2011, 05:47 PM   #6 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 31
sgr_81 is on a distinguished road
Default

I forgot to tell you im receiving this alert



Is that normal?
sgr_81 is offline   Reply With Quote
Old 04-09-2011, 05:49 PM   #7 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Typecast it to avoid the compiler warning, if you are sure that the pointers will be pointing to UIImageView objects.
baja_yu is offline   Reply With Quote
Old 04-09-2011, 05:55 PM   #8 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 31
sgr_81 is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
Typecast it to avoid the compiler warning, if you are sure that the pointers will be pointing to UIImageView objects.
thanks!!!!! it removes the warning, this is the code now:

piezas = [[NSMutableArray alloc] initWithCapacity:kNumeroDePiezas];
for(int i = 0; i < kNumeroDePiezas; i++)
{
UIImageView *anImageView = (UIImageView *)[self.view viewWithTag:i+1];
if (anImageView)
[piezas addObject: anImageView];
}
sgr_81 is offline   Reply With Quote
Old 04-09-2011, 07:26 PM   #9 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by sgr_81 View Post
thanks!!!!! it removes the warning, this is the code now:

piezas = [[NSMutableArray alloc] initWithCapacity:kNumeroDePiezas];
for(int i = 0; i < kNumeroDePiezas; i++)
{
UIImageView *anImageView = (UIImageView *)[self.view viewWithTag:i+1];
if (anImageView)
[piezas addObject: anImageView];
}
If you want to be really careful, you could add a runtime type check to make sure the view you grab is really an image view, like this:

Code:
piezas = [[NSMutableArray alloc] initWithCapacity:kNumeroDePiezas]; 
for(int i = 0; i < kNumeroDePiezas; i++) 
{
  UIImageView *anImageView = (UIImageView *)[self.view viewWithTag:i+1];
  if (anImageView && [anImageView isMemberOfClass: [UIImageView class]])
    [piezas addObject: anImageView];
}
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 04-09-2011, 07:38 PM   #10 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 31
sgr_81 is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
If you want to be really careful, you could add a runtime type check to make sure the view you grab is really an image view, like this:

Code:
piezas = [[NSMutableArray alloc] initWithCapacity:kNumeroDePiezas]; 
for(int i = 0; i < kNumeroDePiezas; i++) 
{
  UIImageView *anImageView = (UIImageView *)[self.view viewWithTag:i+1];
  if (anImageView && [anImageView isMemberOfClass: [UIImageView class]])
    [piezas addObject: anImageView];
}
very nice improvement!!!!! I already applied to my code!!! thanks
sgr_81 is offline   Reply With Quote
Reply

Bookmarks

Tags
for loop, nsmutablearrray, uiimageview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 364
13 members and 351 guests
dansparrow, dre, ilmman, LezB44, michelle, Nobbsy, Objective Zero, oztemel, samdanielblr, Sami Gh, shagor012, sledzeppelin, thephotographer
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,119
Posts: 402,896
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:44 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0