-1

When i declare an image like this: IBOutlet UIImageView *circle in my ViewController.h

How can I then access the "name" circle in my ViewController.m file?

I need this "name" to determine which image i want to show.

UIImageView *selected = (UIImageView *)[touch view];
selected.image = [UIImage imageNamed:@"   circle goes here   "]; 

Code examples are really appreciated :) Thanks in advance.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
thar
  • 1,094
  • 2
  • 13
  • 25
  • 1
    Folks, this is NOT a duplicate of questions asking about the FILENAME of an image. The poster wants to find the name of the instance variable of the IBOutlet that points to the image view. For that you need to use the objective C runtime write some introspection code. Please vote to re-open the question so I can provide an actual answer to the question – Duncan C Dec 20 '13 at 21:48

3 Answers3

0
UIImage *myImage  = [[UIImage alloc] init];

NSString *strProp = NSStringFromSelector(@selector(myImage));

NSLog(@"%@", strProp);

Output:

myImage
Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
  • I don't want to have an if statement for every picture. I search for a more dynamic solution :) – thar Dec 20 '13 at 21:04
0

Rather than searching for the property named circle, dynamically, I would recommend you to set a unique tag properly to circle imageview you have, in your viewDidLoad method or from interface builder, whatever is suitable:

-(void)viewDidLoad {
    //.........
    circle.tag = 999; //or any other value that lets you determine it uniquely 
    //.........
}

And then anywhere in your code, you can determine whether a view is circle uiimageview by just verifying its tag property like this:

UIImageView *selected = (UIImageView *)[touch view];
if(selected.tag == 999) {
    selected.image = [UIImage imageNamed:@"   circle goes here   "]; 
}
Ayan Sengupta
  • 5,238
  • 2
  • 28
  • 40
  • Hi Ayan, thanks for your answer. What is the reason that you don't recommend to get this property name(circle) dynamically? Is it not possible or? – thar Dec 20 '13 at 20:58
  • 1
    For determining a property dynamically from a class, actually you need to use a process called "introspection". For that you need to use "runtime.h" header in your code and introspect a class at runtime by `class_getProperty` function to determine a property, if exists. However that's a hell lot of an overhead to the runtime and i don't recommend it as such to perform a simple task as you were trying. – Ayan Sengupta Dec 20 '13 at 21:07
-1

From the this SO question:

That functionality is not built-in to UIImage because images are not always loaded from files. However, you could create a custom UIImageView subclass to fit your needs.

Community
  • 1
  • 1
esqew
  • 42,425
  • 27
  • 92
  • 132
  • You should flag or closevote this as a duplicate instead. Right now your post doesn't contain any additional value compared to the original one. – Jeroen Vannevel Dec 21 '13 at 02:23