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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.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 11-10-2009, 06:41 AM   #1 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 7
Unhappy Unable to assign images to an imageView from an Array

Hi all, I have a problem with arrays and passing images between views that I would like some help on! So the background is that I have:

• RootViewController (which handles my table view)

• CommunicationViewController which handles the detail of the selected element from the table

• SelectSlideViewController which displays an image clicked on from the CommunicationViewController and allows the user to select a different one from the camera roll

So the problem:

• In the CommunicationViewConroller, I have the following code if the user clicks on a button:


Code:
- (IBAction) selectSlide:(id) sender
{
	if(self.selectSlideView == nil)
	{
	SelectSlideViewController *viewController = [[SelectSlideViewController alloc]
											initWithNibName:@"SelectSlideViewController" bundle:[NSBundle mainBundle]];
		self.selectSlideView = viewController;
		[viewController release];
	}
	
	[self.navigationController pushViewController:self.selectSlideView animated:YES];
	self.selectSlideView.cmn = cmn;
	self.selectSlideView.title = cmn.name;
	self.selectSlideView.imageView.image = self.myImage5;
}
And the above code works, if for, example, I click on button 5, as it sets image5 to the view in the “SelectSlideViewController”.

However, I would like to have multiple buttons using the same “selectSlide” action – and to do that, I need to figure out which button was pressed, and then assign the correct image to the “SelectSlideViewController” from an array of images (or a series of if-else statements which is clunky).

• So my revised code is as follows, but it doesn’t work with the array – any thoughts?:

Code:
- (IBAction) selectSlide:(id) sender
{
	if(self.selectSlideView == nil)
	{
	SelectSlideViewController *viewController = [[SelectSlideViewController alloc]
												 initWithNibName:@"SelectSlideViewController" bundle:[NSBundle mainBundle]];
		self.selectSlideView = viewController;
		[viewController release];
	}
	
	NSUInteger tmpInt = -1;
	tmpInt = [buttonArray indexOfObject:sender];
	
	[self.navigationController pushViewController:self.selectSlideView animated:YES];
	self.selectSlideView.cmn = cmn;
	self.selectSlideView.title = cmn.name;
	
	NSLog(@"The int was %d",tmpInt);
	NSLog(@"This is the image array size %d ",[imageArray count]);	
	
	If(tmpInt >-1 && tmpInt <9)
	{
		self.selectSlideView.imageView.image = [imageArray objectAtIndex:tmpInt];
	}
	/** this code works, but is a bit clunky:
	if(tmpInt == 0)
		self.selectSlideView.imageView.image = self.myImage1;
	else if (tmpInt == 1)
		self.selectSlideView.imageView.image = self.myImage2;
	else if (tmpInt == 2)
		self.selectSlideView.imageView.image = self.myImage3;
	else if (tmpInt == 3)
		self.selectSlideView.imageView.image = self.myImage4;
	else if (tmpInt == 4)
		self.selectSlideView.imageView.image = self.myImage5;
	else if (tmpInt == 5)
		self.selectSlideView.imageView.image = self.myImage6;
	else if (tmpInt == 6)
		self.selectSlideView.imageView.image = self.myImage7;
	else if (tmpInt == 7)
		self.selectSlideView.imageView.image = self.myImage8;
	else if (tmpInt == 8)
		self.selectSlideView.imageView.image = self.myImage9;	
	**/
}
trilithium is offline   Reply With Quote
Old 11-10-2009, 06:54 AM   #2 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,526
Default

There is a "tag" property that you can assign to each button. It is an integer, and it will make it unnecessary to anything so clunky as using indexOfObject to determine which button was clicked. You can assign the tag value at the time that the button is created, or, if you are using IB to create the buttons, there is a tag field you can fill out.

Robert Scott
Ypsilanti, Michigan
RLScott is offline   Reply With Quote
Old 11-10-2009, 07:24 AM   #3 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 7
Default

Hi Scott,

Thanks for that - I will use that feature rather than going through an array to find out which button was clicked. However, how do I assign the correct image from a different array to the new UIImageView, based on which button was clicked (remember, I have 2 arrays, one that corresponds to the buttons, and other that corresponds to the images displayed on the buttons).

Regards.
trilithium is offline   Reply With Quote
Old 11-10-2009, 11:52 AM   #4 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 7
Default

Here is some more detail:


In my header file, i have the following entry: @property(nonatomic, retain) NSMutableArray * imageArray;

Code:
- (void) updateButtonImages
{
UIImage myImage1 = [UIImage imageNamed:@"image1.jpg"]; 
[imageArray addObject:myImage1]; 
}
This is the code that does something with the images:
Code:
- (void) selectSlide
{
self.selectSlideView.imageView.image = myImage1; //this works
self.selectSlideView.imageView.image = [imageArray objectAtIndex:1]; //doesn't work 
NSLog(@"This is the image array size %d ",[imageArray count]); //this outputs value of "9" 
}
So it could be that the imageArray is not being retained - but how do I ensure it is being retained?


Quote:
Originally Posted by trilithium View Post
Hi Scott,

Thanks for that - I will use that feature rather than going through an array to find out which button was clicked. However, how do I assign the correct image from a different array to the new UIImageView, based on which button was clicked (remember, I have 2 arrays, one that corresponds to the buttons, and other that corresponds to the images displayed on the buttons).

Regards.
trilithium is offline   Reply With Quote
Old 11-10-2009, 04:15 PM   #5 (permalink)
Humbled Student
 
Dutch's Avatar
 
Join Date: Apr 2009
Location: Long Island, NY
Age: 32
Posts: 883
Send a message via AIM to Dutch
Default

Code:
- (void) updateButtonImages
{
UIImage myImage1 = [UIImage imageNamed:@"image1.jpg"]; 
[imageArray addObject:myImage1]; 
}
should be

Code:
- (void) updateButtonImages
{
UIImage *myImage1 = [UIImage imageNamed:@"image1.jpg"]; 
[imageArray addObject:myImage1]; 
}
Since a UIImage is an Object, it requires the asterisk.
Dutch is offline   Reply With Quote
Old 11-11-2009, 01:08 AM   #6 (permalink)
Beast Mode
 
Join Date: Dec 2008
Age: 21
Posts: 1,890
Default

try casting it

Code:
- (void) selectSlide
{
self.selectSlideView.imageView.image = myImage1; //this works
self.selectSlideView.imageView.image = (UIImage *)[imageArray objectAtIndex:1]; //doesn't work 
NSLog(@"This is the image array size %d ",[imageArray count]); //this outputs value of "9" 
}
__________________
I really do this.
Bertrand21 is offline   Reply With Quote
Old 11-11-2009, 03:27 PM   #7 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 7
Default

Hi.

Sorry - it was *myImage1 (when i was typing the code in the post, i missed it out).



Quote:
Originally Posted by Dutch View Post
Code:
- (void) updateButtonImages
{
UIImage myImage1 = [UIImage imageNamed:@"image1.jpg"]; 
[imageArray addObject:myImage1]; 
}
should be

Code:
- (void) updateButtonImages
{
UIImage *myImage1 = [UIImage imageNamed:@"image1.jpg"]; 
[imageArray addObject:myImage1]; 
}
Since a UIImage is an Object, it requires the asterisk.
trilithium is offline   Reply With Quote
Old 11-11-2009, 03:32 PM   #8 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 7
Default

Further to this post, I have just realised the following:

It turns out that using the array method does work, however I observe the following behaviour: -

I click on a button which should diplay a new view with a larger version of the image on the button - When I run the simulator, the first button press always results in an image not being displayed in the new view - However if I go back and click on the same button (or indeed another button) for all subsequent clicks, the correct image is displayed.

Only the first button press fails (and i have tried different buttons, but same result).

So for whatever reason, it takes two attempts before the imageView.image is assigned:

Code:
self.selectSlideView.imageView.image = [imageArray objectAtIndex:tmpInt];

Quote:
Originally Posted by trilithium View Post
Hi all, I have a problem with arrays and passing images between views that I would like some help on! So the background is that I have:

• RootViewController (which handles my table view)

• CommunicationViewController which handles the detail of the selected element from the table

• SelectSlideViewController which displays an image clicked on from the CommunicationViewController and allows the user to select a different one from the camera roll

So the problem:

• In the CommunicationViewConroller, I have the following code if the user clicks on a button:


Code:
- (IBAction) selectSlide:(id) sender
{
	if(self.selectSlideView == nil)
	{
	SelectSlideViewController *viewController = [[SelectSlideViewController alloc]
											initWithNibName:@"SelectSlideViewController" bundle:[NSBundle mainBundle]];
		self.selectSlideView = viewController;
		[viewController release];
	}
	
	[self.navigationController pushViewController:self.selectSlideView animated:YES];
	self.selectSlideView.cmn = cmn;
	self.selectSlideView.title = cmn.name;
	self.selectSlideView.imageView.image = self.myImage5;
}
And the above code works, if for, example, I click on button 5, as it sets image5 to the view in the “SelectSlideViewController”.

However, I would like to have multiple buttons using the same “selectSlide” action – and to do that, I need to figure out which button was pressed, and then assign the correct image to the “SelectSlideViewController” from an array of images (or a series of if-else statements which is clunky).

• So my revised code is as follows, but it doesn’t work with the array – any thoughts?:

Code:
- (IBAction) selectSlide:(id) sender
{
	if(self.selectSlideView == nil)
	{
	SelectSlideViewController *viewController = [[SelectSlideViewController alloc]
												 initWithNibName:@"SelectSlideViewController" bundle:[NSBundle mainBundle]];
		self.selectSlideView = viewController;
		[viewController release];
	}
	
	NSUInteger tmpInt = -1;
	tmpInt = [buttonArray indexOfObject:sender];
	
	[self.navigationController pushViewController:self.selectSlideView animated:YES];
	self.selectSlideView.cmn = cmn;
	self.selectSlideView.title = cmn.name;
	
	NSLog(@"The int was %d",tmpInt);
	NSLog(@"This is the image array size %d ",[imageArray count]);	
	
	If(tmpInt >-1 && tmpInt <9)
	{
		self.selectSlideView.imageView.image = [imageArray objectAtIndex:tmpInt];
	}
	/** this code works, but is a bit clunky:
	if(tmpInt == 0)
		self.selectSlideView.imageView.image = self.myImage1;
	else if (tmpInt == 1)
		self.selectSlideView.imageView.image = self.myImage2;
	else if (tmpInt == 2)
		self.selectSlideView.imageView.image = self.myImage3;
	else if (tmpInt == 3)
		self.selectSlideView.imageView.image = self.myImage4;
	else if (tmpInt == 4)
		self.selectSlideView.imageView.image = self.myImage5;
	else if (tmpInt == 5)
		self.selectSlideView.imageView.image = self.myImage6;
	else if (tmpInt == 6)
		self.selectSlideView.imageView.image = self.myImage7;
	else if (tmpInt == 7)
		self.selectSlideView.imageView.image = self.myImage8;
	else if (tmpInt == 8)
		self.selectSlideView.imageView.image = self.myImage9;	
	**/
}
trilithium is offline   Reply With Quote
Old 11-12-2009, 04:50 PM   #9 (permalink)
Humbled Student
 
Dutch's Avatar
 
Join Date: Apr 2009
Location: Long Island, NY
Age: 32
Posts: 883
Send a message via AIM to Dutch
Default

Just out of curiousity, does this yield the same error?


Replace This

Code:
self.selectSlideView.imageView.image = [imageArray objectAtIndex:tmpInt];
with

Code:
[[[self selectSlideView] imageView] setImage:(UIImage *)[imageArray objectAtIndex:tmpInt]];
Dutch is offline   Reply With Quote
Old 11-12-2009, 04:55 PM   #10 (permalink)
jsd
at this moment
 
Join Date: Mar 2009
Location: San Francisco, CA
Posts: 900
Default

Quote:
Originally Posted by trilithium View Post
I click on a button which should diplay a new view with a larger version of the image on the button - When I run the simulator, the first button press always results in an image not being displayed in the new view - However if I go back and click on the same button (or indeed another button) for all subsequent clicks, the correct image is displayed.
is selectSlideView.imageView loaded from a nib? if so, that's the problem. the view isn't loaded the first time through.

try putting
[selectSlideView view];
before setting the imageview to force the nib loading to happen.
jsd is offline   Reply With Quote
Old 11-16-2009, 01:24 AM   #11 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 7
Default

Hi,

Sorry for the delay in replying - work has been hectic so haven't been able to devote some time to the iPhone app.

Right, I tried the revised code below and it still yields the same result.



Quote:
Originally Posted by Dutch View Post
Just out of curiousity, does this yield the same error?


Replace This

Code:
self.selectSlideView.imageView.image = [imageArray objectAtIndex:tmpInt];
with

Code:
[[[self selectSlideView] imageView] setImage:(UIImage *)[imageArray objectAtIndex:tmpInt]];
trilithium is offline   Reply With Quote
Old 11-16-2009, 01:27 AM   #12 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 7
Default

jsd,

You were spot on! Looks like this has resolved the issue. Once again, apologies for the delay in replying, but haven't had a chance to devote time to the iPhone app.

Thanks, and best Regards.

Quote:
Originally Posted by jsd View Post
is selectSlideView.imageView loaded from a nib? if so, that's the problem. the view isn't loaded the first time through.

try putting
[selectSlideView view];
before setting the imageview to force the nib loading to happen.
trilithium is offline   Reply With Quote
Reply

Bookmarks

Tags
nsmutablearray, uiimageview and uiimage

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: 254
16 members and 238 guests
ADY, Alsahir, dacapo, Dani77, Desert Diva, djohnson, F_Bryant, Grinarn, HemiMG, jansan, M@realobjects, macquitzon216, prchn4christ, smethorst, spiderguy84
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,882
Threads: 89,228
Posts: 380,762
Top Poster: BrianSlick (7,129)
Welcome to our newest member, jansan
Powered by vBadvanced CMPS v3.1.0

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