miDark
miDark

Reputation: 83

Can we get UILabel's name?

I was wondering if it is possible to get the name of a UILabel (not the text).

Something like this:

if([label.name is equalToString:key]){
     //do something
}

I know label.name doesn't exist but maybe there is something equivalent. How can I do this?

Upvotes: 5

Views: 1032

Answers (1)

Wyetro
Wyetro

Reputation: 8588

You could subclass UILabel:

// Objective-C

#import <UIKit/UIKit.h>

@interface NMLabel : UILabel

@property (nonatomic, readwrite) NSString *name;

@end


// Swift
import UIKit

class NMLabel : UILabel {

    var name : String = ""

}

Or at the most basic of levels you can use the already existent tag property (in both Objective-C or Swift):

label.tag = 5

// Objective-C

NSLog(@"%d", label.tag); // prints 5

// Swift

print(label.tag) // prints 5

Upvotes: 4

Related Questions