 |
 |
|
 |
11-19-2009, 09:23 AM
|
#1 (permalink)
|
|
Registered Member
Join Date: Jul 2009
Posts: 21
|
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 (nonatomic, retain) IBOutlet UIImageView *q0;
@property (nonatomic, retain) IBOutlet UIImageView *q1;
@property (nonatomic, retain) IBOutlet UIImageView *q2;
@property (nonatomic, retain) IBOutlet UIImageView *q3;
@property (nonatomic, retain) IBOutlet UIImageView *q4;
@property (nonatomic, retain) IBOutlet UIImageView *q5;
@property (nonatomic, retain) IBOutlet UIImageView *q6;
@property (nonatomic, retain) IBOutlet UIImageView *q7;
@property (nonatomic, retain) IBOutlet UIImageView *q8;
@property (nonatomic, retain) IBOutlet UIImageView *q9;
...
- (void) changeProgressionImages:(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;
...
-(void) changeProgressionImages:(BOOL) correct
{
NSString *imageLocation;
if( correct )
imageLocation = correctAnswerImage;
else
imageLocation = wrongAnswerImage;
if( currentQuestionNumber == 0 )
{
q0.image = [UIImage imageNamed: imageLocation];
q1.image = [UIImage imageNamed: currentAnswerImage];
}
else if( currentQuestionNumber == 1 )
{
q1.image = [UIImage imageNamed: imageLocation];
q2.image = [UIImage imageNamed: currentAnswerImage];
}
else if( currentQuestionNumber == 2 )
{
q2.image = [UIImage imageNamed: imageLocation];
q3.image = [UIImage imageNamed: currentAnswerImage];
}
else if( currentQuestionNumber == 3 )
{
q3.image = [UIImage imageNamed: imageLocation];
q4.image = [UIImage imageNamed: currentAnswerImage];
}
else if( currentQuestionNumber == 4 )
{
q4.image = [UIImage imageNamed: imageLocation];
q5.image = [UIImage imageNamed: currentAnswerImage];
}
else if( currentQuestionNumber == 5 )
{
q5.image = [UIImage imageNamed: imageLocation];
q6.image = [UIImage imageNamed: currentAnswerImage];
}
else if( currentQuestionNumber == 6 )
{
q6.image = [UIImage imageNamed: imageLocation];
q7.image = [UIImage imageNamed: currentAnswerImage];
}
else if( currentQuestionNumber == 7 )
{
q7.image = [UIImage imageNamed: imageLocation];
q8.image = [UIImage imageNamed: currentAnswerImage];
}
else if( currentQuestionNumber == 8 )
{
q8.image = [UIImage imageNamed: imageLocation];
q9.image = [UIImage imageNamed: currentAnswerImage];
}
else
{
q9.image = [UIImage imageNamed: imageLocation];
}
}
...
@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;
...
-(void) changeProgressionImages:(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
getObjectByName( gradedImageName ).image = [UIImage imageNamed: imageLocation];
getObjectByName( currentImageName ).image = [UIImage imageNamed: currentAnswerImage];
}
I'm wondering if it this is possible.
|
|
|
11-19-2009, 10:06 AM
|
#2 (permalink)
|
|
Senior Member
iPhone Dev SDK Supporter
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,575
|
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];
__________________
|
|
|
11-19-2009, 11:08 AM
|
#3 (permalink)
|
|
Registered Member
Join Date: Jul 2009
Posts: 21
|
Quote:
Originally Posted by smasher
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 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 = [UIImage imageNamed: imageLocation];
UIImageView *currrentImage = [imageViews objectAtIndex:currentQuestionNumber+1];
currrentImage.image = [UIImage imageNamed: currentAnswerImage];
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?
|
|
|
11-19-2009, 02:00 PM
|
#4 (permalink)
|
|
Senior Member
iPhone Dev SDK Supporter
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,575
|
Quote:
Originally Posted by icelandic
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.
__________________
|
|
|
11-19-2009, 02:04 PM
|
#5 (permalink)
|
|
Registered Member
Join Date: Jul 2009
Posts: 21
|
Quote:
Originally Posted by smasher
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.
|
|
|
11-19-2009, 03:32 PM
|
#6 (permalink)
|
|
Senior Member
iPhone Dev SDK Supporter
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,575
|
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.
__________________
|
|
|
11-19-2009, 04:29 PM
|
#7 (permalink)
|
|
Registered Member
Join Date: Jul 2009
Posts: 21
|
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 imageNamed: imageLocation];
but I would like to change the image like this sudo code:
PHP Code:
NSString *objectName = @"q0";
id imageID = [objectName tag];
UIImageView *currentObject = [UIImageView initWithId: imageID];
currentObject.image = [UIImage imageNamed: imageLocation];
|
|
|
11-25-2009, 09:21 AM
|
#8 (permalink)
|
|
Registered Member
Join Date: Jul 2009
Posts: 21
|
bump
|
|
|
11-25-2009, 11:53 AM
|
#9 (permalink)
|
|
at this moment
Join Date: Mar 2009
Location: San Francisco, CA
Posts: 870
|
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.
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
» Online Users: 256 |
| 27 members and 229 guests |
| Abel, aniuco, benoitr007, Breshi, CHV, cparker101, csnplt, dapis, DenVog, Duncan C, firearasi, flamingliquid, gbaldwin9, HemiMG, HoofSC, lepetitapps, markph, mer, m_kaminsky@yahoo.com, RJaus, sarahconnor, seymores, shul, smilespray, upperhouse, wardyfloyd |
| Most users ever online was 779, 05-11-2009 at 09:55 AM. |
» Stats |
Members: 24,267
Threads: 39,062
Posts: 171,292
Top Poster: smasher (2,575)
|
| Welcome to our newest member, nvrendingsoft |
|