我试图继承一些 UIButtons 以获得类似于 button.tag 属性的属性。在此属性中,我想设置一个 MCPeerID。该属性必须类似于:
button.thePeerID = an MCPeerID
if (button.thePeerID == a peer id)...
不幸的是,标签属性只能保存数字。我知道我必须添加一个 UIButton 类型的新文件并像这样调用它:
SubclassButton *myButton=[SubclassButton buttonWithType:UIButtonTypeRoundedRect];
但是我如何获得所需的属性呢?
最佳答案
您将在头文件 (subclassButton.h) 中设置该属性
@interface SubclassButton : UIButton
@property (nonatomic, strong) NSString *thePeerId;
@end
...然后您可以按照上面的建议访问它:
myButton.thePeerId = @"abcd";
(当然,类型取决于McPeerID实际是什么。相应地调整)
关于ios - 子类化 UIButton 以像标签一样设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19870006/