Search code examples
iosuiviewuielement

Access human readable UIElement label from IOS app


I've got a handful of UIViews subclasses as a part of my interface and I'm looking for an easy human readable way to differentiate them in the code -- like the label you set in the "Document" section of the UI editor.

I believe the "Accessibility Label" is exposed, but that doesn't seem like the correct use of that variable.

Am I stuck documenting which Object ID each object has or is there a more intelligible way?


Solution

  • There are lots of ways to do what you want to be done. Here are several of them:

    1. @properties

      The first thing that came to my mind are properties. Use your view controller as a storage class by adding following property declarations in your header file:

      @property (nonatomic, weak) IBOutlet UIView *myFantasticView;
      @property (nonatomic, weak) IBOutlet UIView *myGorgeousView;
      ...
      

      Then, just simply connect your views to specific outlets in Interface Builder.

      The only drawback is that you will have to declare a lot of properties which can become a little confusing.

    2. Custom runtime attributes (another use of @properties)

      If you're using UIView's subclasses (I assume you do), you can use your UIView subclass and declare an "identifier" property:

      @interface MyView : UIView
      
      @property (nonatomic, strong) NSString *myViewID;
      
      ...
      
      @end
      

      Then, assign this value using code (simple property setting) or in Interface Builder:

      interface builder screenshot

    Now you can identify your views using one of those method. But remember that you can come up with a better, more suitable solution! :)