2

I need get object name for UILabel, UITextView, etc. example:

@property (nonatomic, strong) IBOutlet UITextField *Adress;

...

for (UIView *theObject in [self.view subviews]){
   NSString *objName = //theObject name 
   NSLog(@"name of subview is: %@", objName);
   // should be "name of subview is: Adress  "
}

Anyone have any ideas?

Alex
  • 2,100
  • 19
  • 26
  • what name you are talking about Class Name or IBoutlet name – iGagan Kumar Nov 07 '13 at 12:19
  • why would you like to have the name of an outlet? You could tag it, and set an arbitrary name. – Mikael Nov 07 '13 at 12:19
  • 2
    Please try to stick to Objective-C naming conventions. Starting variable names with uppercase characters (`Adress`) isn't a good idea as by convention, constants and class names start with uppercase characters. – DarkDust Nov 07 '13 at 12:25
  • 2
    Instead of asking this question, it's better if you tell us what **high level problem** you're trying to solve and we can tell how to do _that_. – DarkDust Nov 07 '13 at 12:36

4 Answers4

2

You can't get the name you gave your pointer at run time. If however, you're interested in getting the class of the object, that is more doable..

for (UIView *view in self.view.subviews) {
    NSString *className = NSStringFromClass([view class]);
    NSLog(@"the name of the class of the current object is: %@",className);
}

If you do really want to be able to reference the object like this, I suggest you instead utilize UIView's tag property to keep track of your views. Or, if you insist on doing this by string, you could always subclass your elements and give them an NSString name property. Doing this, you could just hard code these values to be equivalent to the string value of the name of the pointer you assigned to the object.

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
1

You cannot get the name of the variable that points to your object (okay, you can, but see below).

This is a bit like a regular street address: if you have a mail envelope (variable) you can see to what address it points to, but how would you be able to find all envelopes that point to the same address? You would need to gather all envelopes and scan them all or add some kind of tracking.

In Objective-C, there is a way of walking all instance variables of an object and check whether they point to your object. But this is very low level and a total anti-pattern. Also, you'd need to know the objects whose instance variables you'd like to enumerate.

So answer is: even though it's possible in Objective-C to a certain degree, don't do it. Change your design instead to not rely on this information.

DarkDust
  • 90,870
  • 19
  • 190
  • 224
1

This task is not pretty clear.

First of all, you should understand that one object may be referred from several properties.

By the way, you can try to scan view tree and list of all ViewControllers and lookup for all properties which refer to this object.

Tree scan may be implemented via recursive function. How to find all UIViews?

ViewController list scan can be implemented via How to get all view controllers?

Property scan may be implemented via reflection which is described in the following topic

Probably, you can try to scan all references to the object and scan their properties. However, I don't see any quick guide for that task right know.

Community
  • 1
  • 1
Ivan Surzhenko
  • 149
  • 1
  • 8
1

You can subclass the UI class and add a name field to it, then set that name at initialization time. Or use the tag value and some lookup method such as a NSDictionary. Or add an associative reference.

But trying to use the variable name is not the best solution, what are you trying to accomplish?

zaph
  • 111,848
  • 21
  • 189
  • 228