Yes, you can. (c) :)
For the solution that I suggest you work with, you will need to have access (set / get) your variables using methods (easy to make using properties or write your own setters and getters).
Here is an example:
- (void)performAction:(NSMutableString *)text { [(UIImageView *)[self performSelector:NSSelectorFromString([text stringByAppendingString:@"Pic1"])] setHidden:YES]; [(UIImageView *)[self performSelector:NSSelectorFromString([text stringByAppendingString:@"Pic2"])] setHidden:NO]; }
In terms of properties, I was actually thinking of just giving you sample code to start using this wonderful Objective-C ASAP feature, which I will do. Although I am not sure to delve into this topic, because it may require too much Internet paper, therefore, why, after the example code, I will also add some links for further reading.
@interface AClass : NSObject { // Here where you declare variables NSObject *objectForInternalUseWeWantToBeRetained; id linkToObjectUsuallyNotRetained; int nonObjectVariable; BOOL aVariableWithARenamedGetter; } // And here where their properties are declared @property (nonatomic, retain) NSObject *objectForInternalUseWeWantToBeRetained; @property (nonatomic) id linkToObjectUsuallyNotRetained; @property (nonatomic, assign) int nonObjectVariable; @property (nonatomic, assign, getter=renamedVariableGetter) BOOL aVariableWithARenamedGetter; @end @implementation AClass // Here we command the machine to generate getters/setters for these properties automagically @synthesize objectForInternalUseWeWantToBeRetained, linkToObjectUsuallyNotRetained, nonObjectVariable, aVariableWithARenamedGetter; // But you can implement any of the getters/setters by yourself to add some additional behaviour - (NSObject *)objectForInternalUseWeWantToBeRetained { // Some additional non-usual stuff here // And after that we do the getters job - return the variables value return objectForInternalUseWeWantToBeRetained; } // And of course we don't forget to release all the objects we retained on dealloc - (void)dealloc { [objectForInternalUseWeWantToBeRetained release]; [super dealloc]; } @end // And here where their properties are declared @property (nonatomic, retain) UIImageView *testPic1; @property (nonatomic, retain) UIImageView *testPic2; @end
Warning: I quickly went through the day. Here is a good tutorial on CocoaCast's blog - Properties in Objective-C 2.0 , which I think can be a good starting point. By the way, they provide a lot of training materials (podcasts, screencasts, etc.), so viewing their site can be useful. And, of course, the main place to learn all about Objective-C and Cocoa is the official documentation, here where it is about properties .
source share