我们能得到 UILabel 的名字吗?

Can we get UILabel's name?

我想知道是否可以获得 UILabel 的名称(不是文本)。

像这样:

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

我知道 label.name 不存在,但也许有类似的东西。我该怎么做?

你可以继承 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 = ""

}

或者在最基本的级别上,您可以使用已经存在的 tag 属性(在 Objective-C 或 Swift 中):

label.tag = 5

// Objective-C

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

// Swift

print(label.tag) // prints 5