Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone
($4.99)

Shape Up
($0.99)

Your First iPhone App
($1.99)

iVidCam Free
(free)

Kid Art
($0.99)

iPUBQUIZ
(£1.19)

ArtStudio
($3.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-19-2009, 09:23 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 21
Default Get Object by name

I'm trying to change images in an app, but I want the iPhone to build the name of the object to change and then change it. Here's what I currently have but want to change...

.h file
PHP Code:
@interface implementation NSObject <UIAlertViewDelegate>{
...
    
IBOutlet UIImageView *q0;
    
IBOutlet UIImageView *q1;
    
IBOutlet UIImageView *q2;
    
IBOutlet UIImageView *q3;
    
IBOutlet UIImageView *q4;
    
IBOutlet UIImageView *q5;
    
IBOutlet UIImageView *q6;
    
IBOutlet UIImageView *q7;
    
IBOutlet UIImageView *q8;
    
IBOutlet UIImageView *q9;

...
}

@
property (nonatomicretainIBOutlet UIImageView *q0;
@
property (nonatomicretainIBOutlet UIImageView *q1;
@
property (nonatomicretainIBOutlet UIImageView *q2;
@
property (nonatomicretainIBOutlet UIImageView *q3;
@
property (nonatomicretainIBOutlet UIImageView *q4;
@
property (nonatomicretainIBOutlet UIImageView *q5;
@
property (nonatomicretainIBOutlet UIImageView *q6;
@
property (nonatomicretainIBOutlet UIImageView *q7;
@
property (nonatomicretainIBOutlet UIImageView *q8;
@
property (nonatomicretainIBOutlet UIImageView *q9;

...
- (
voidchangeProgressionImages:(BOOL)correct;
...

@
end 
.m file
PHP Code:
static NSString *correctAnswerImage = @"fffl_correct.png";
static 
NSString *wrongAnswerImage = @"fffl_wrong.png";
static 
NSString *currentAnswerImage = @"fffl_current.png";

@
implementation implementation

@synthesize q0;
@
synthesize q1;
@
synthesize q2;
@
synthesize q3;
@
synthesize q4;
@
synthesize q5;
@
synthesize q6;
@
synthesize q7;
@
synthesize q8;
@
synthesize q9;

int currentQuestionNumber 0;
...

-(
voidchangeProgressionImages:(BOOL) correct    
{    
    
NSString *imageLocation;
    
    if( 
correct )
        
imageLocation correctAnswerImage;
    else
        
imageLocation wrongAnswerImage;

    if( 
currentQuestionNumber == )
    {
        
q0.image = [UIImage imageNamedimageLocation];
        
q1.image = [UIImage imageNamedcurrentAnswerImage]; 
    }
    else if( 
currentQuestionNumber == )
    {
        
q1.image = [UIImage imageNamedimageLocation];
        
q2.image = [UIImage imageNamedcurrentAnswerImage];
    }
    else if( 
currentQuestionNumber == )
    {
        
q2.image = [UIImage imageNamedimageLocation];
        
q3.image = [UIImage imageNamedcurrentAnswerImage];
    }
    else if( 
currentQuestionNumber == )
    {
        
q3.image = [UIImage imageNamedimageLocation];
        
q4.image = [UIImage imageNamedcurrentAnswerImage];
    }
    else if( 
currentQuestionNumber == )
    {
        
q4.image = [UIImage imageNamedimageLocation];
        
q5.image = [UIImage imageNamedcurrentAnswerImage];
    }
    else if( 
currentQuestionNumber == )
    {
        
q5.image = [UIImage imageNamedimageLocation];
        
q6.image = [UIImage imageNamedcurrentAnswerImage];
    }
    else if( 
currentQuestionNumber == )
    {
        
q6.image = [UIImage imageNamedimageLocation];
        
q7.image = [UIImage imageNamedcurrentAnswerImage];
    }
    else if( 
currentQuestionNumber == )
    {
        
q7.image = [UIImage imageNamedimageLocation];
        
q8.image = [UIImage imageNamedcurrentAnswerImage];
    }
    else if( 
currentQuestionNumber == )
    {
        
q8.image = [UIImage imageNamedimageLocation];
        
q9.image = [UIImage imageNamedcurrentAnswerImage];
    }
    else
    {
        
q9.image = [UIImage imageNamedimageLocation];
    }
}
...
@
end 

Instead of having all that code in the changeProgressionImages method, I would like to something like this:

PHP Code:
//Incremented when question number changes
int currentQuestionNumber 0;

...

-(
voidchangeProgressionImages:(BOOL) correct    
{    
    
NSString *imageLocation;
    
    if( 
correct )
        
imageLocation correctAnswerImage;
    else
        
imageLocation wrongAnswerImage;

    
NSString *gradedImageName = [NSString stringWithFormat: @"q%d"currentQuestionNumber];
    
NSString *currentImageName = [NSString stringWithFormat: @"q%d"currentQuestionNumber+1];

//I know this is a javascript like reference
    
getObjectByNamegradedImageName ).image = [UIImage imageNamedimageLocation];

    
getObjectByNamecurrentImageName ).image = [UIImage imageNamedcurrentAnswerImage];


I'm wondering if it this is possible.
icelandic is offline   Reply With Quote
Old 11-19-2009, 10:06 AM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,575
Default

It's possible, but it's not what you want here - you should use an array instead. You can add all of your object to an NSArray (or NSMutableArray if you need to modify the list) and access them like this:

Code:
//add imageViews to the array - do this once
NSArray *imageViews = [[NSArray alloc] initWithObjects: q0, q1, q2, q3, q4, q5, q6, q7, q8, q9, nil];

//pull imageviews from the array
UIImageview *gradedImage = [imageViews objectAtIndex:currentQuestionNumber];
gradedImage.image = [imageViews imageNamed: imageLocation];

UIImageview *currrentImage = [myArray objectAtIndex:currentQuestionNumber+1];
currentImage.image = [UIImage imageNamed: currentAnswerImage];
__________________
smasher is offline   Reply With Quote
Old 11-19-2009, 11:08 AM   #3 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 21
Default

Quote:
Originally Posted by smasher View Post
It's possible, but it's not what you want here - you should use an array instead. You can add all of your object to an NSArray (or NSMutableArray if you need to modify the list) and access them like this:

Code:
//add imageViews to the array - do this once
NSArray *imageViews = [[NSArray alloc] initWithObjects: q0, q1, q2, q3, q4, q5, q6, q7, q8, q9, nil];

//pull imageviews from the array
UIImageview *gradedImage = [imageViews objectAtIndex:currentQuestionNumber];
gradedImage.image = [imageViews imageNamed: imageLocation];

UIImageview *currrentImage = [myArray objectAtIndex:currentQuestionNumber+1];
currentImage.image = [UIImage imageNamed: currentAnswerImage];
I had to tweek the code a little but it's better than I got:
PHP Code:
    //add imageViews to the array - do this once
    
NSArray *imageViews = [[NSArray allocinitWithObjectsq0q1q2q3q4q5q6q7q8q9nil];
    
    
//pull imageviews from the array
    
UIImageView *gradedImage = [imageViews objectAtIndex:currentQuestionNumber];
    
gradedImage.image = [UIImage imageNamedimageLocation];
    
    
UIImageView *currrentImage = [imageViews objectAtIndex:currentQuestionNumber+1];
    
currrentImage.image = [UIImage imageNamedcurrentAnswerImage]; 
I think I can solve this problem fully if I knew how to get the object id by only knowing the object name as a string. Is that possible?
icelandic is offline   Reply With Quote
Old 11-19-2009, 02:00 PM   #4 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,575
Default

Quote:
Originally Posted by icelandic View Post
I had to tweak the code a little but it's better than I got:

I think I can solve this problem fully if I knew how to get the object id by only knowing the object name as a string. Is that possible?
Sorry, I had some typos. Your fixed code looks good. Is it not working?

You can get variables by name using Key-Value coding, but it's *not* the right thing to do here - it's slow and it's overkill.
__________________
smasher is offline   Reply With Quote
Old 11-19-2009, 02:04 PM   #5 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 21
Default

Quote:
Originally Posted by smasher View Post
Sorry, I had some typos. Your fixed code looks good. Is it not working?

You can get variables by name using Key-Value coding, but it's *not* the right thing to do here - it's slow and it's overkill.
The fixed code is working great ( no errors or warnings ). I'm trying to make this method work in such a way where I don't need to hard code the objects in. Also, I'm originally a web programmer so I'm trying to associate how things are done with a framework vs. raw code.
icelandic is offline   Reply With Quote
Old 11-19-2009, 03:32 PM   #6 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,575
Default

If you still want to create the UIImageViews in Interface Builder but you don't want all of those vars / outlets, you can giver them all tags in IB and then locate them using [self.view viewWithTag:x] for adding to the array.

If you want to create them entirely in code, you can use initWithFrame: to create them , then add them as subviews in the current view and add them to the array.
__________________
smasher is offline   Reply With Quote
Old 11-19-2009, 04:29 PM   #7 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 21
Default

More or less, I want to have a string called "q0" and I want to get the id for the UIImageView object named q0. Google is not my friend for this problem.

I can change an image like this:

PHP Code:
q0.image = [UIImage imageNamedimageLocation]; 
but I would like to change the image like this sudo code:
PHP Code:
NSString *objectName = @"q0";
id imageID = [objectName tag];
UIImageView *currentObject = [UIImageView initWithIdimageID];
currentObject.image = [UIImage imageNamedimageLocation]; 
icelandic is offline   Reply With Quote
Old 11-25-2009, 09:21 AM   #8 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 21
Default

bump
icelandic is offline   Reply With Quote
Old 11-25-2009, 11:53 AM   #9 (permalink)
jsd
at this moment
 
Join Date: Mar 2009
Location: San Francisco, CA
Posts: 870
Default

Smasher is right - you should do this with arrays. Just create an array of imageview objects, and if you need a filename that matches the position of the array index, you can use stringWithFormat to generate it.
jsd is offline   Reply With Quote
Reply

Bookmarks

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


Enter the iPhone App Challenge!  Win $500!
» Advertisements
» Stats
Members: 24,267
Threads: 39,062
Posts: 171,292
Top Poster: smasher (2,575)
Welcome to our newest member, nvrendingsoft
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 08:52 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0