I’m new at objective-c and stuck with a problem. Is it possible to take an argument of a function to use as some variable names?
For example, say I have a bunch of image: aPic1, aPic2, bPic1, bPic2, cPic1, cPic2, and I want to create an action so that every time when a button is clicked, the view controller will hide Pic1 and display Pic2, depending which button is clicked.
- (void) performAction:(NSMutableString *) text
{
[text appendString:@"Pic1"].hidden = YES; //I wanna hide textPic1, which is a UIImageView
[text appendString:@"Pic2"].hidden = NO; //I wanna show textPic2, which is also a UIImageView
}
I know I should not use NSString or NSMutableString in this case. Anyone has any idea how I can achieve my objective with this kind of function? Thanks.
Yes you can. (c) 🙂
For the solution I suggest to work you will need to be able to access (set/get) your variables via methods (easily done using properties or writing your own setters and getters).
Here’s an example:
As for properties I actually thought about simply giving you a sample code to get start using this great feature of Objective-C ASAP, which I will do. Though I’m not sure about going deep into this topic because it may require too much of Internet paper, so that’s why after the sample code I’ll also add a few links for further reading.
Warning: I ran through the suff very quickly. Here’s a nice tutorial at CocoaCast blog – Properties in Objective-C 2.0, which I think might be a good starting point. BTW they provide a lot of learning materials (podcasts, screencasts, etc) so browsing their site further might be useful. And of course the main place to learn all about Objective-C and Cocoa is official documentation, here’s where it is about properties.