ios - 子类化 UIButton 以像标签一样设置值

标签 ios uibutton

我试图继承一些 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/

相关文章:

ios - 只有 UIButton 的中心是可点击的

ios - XCode4代码需要签名3次吗?

ios - 无法取消菜单选择 IOS - React Native

objective-c - UIButton 自定义字体未显示

iOS-UIButton系统选择图像

ios - ScrollView 改变按钮 backgroundImage 时跳转到页面顶部

ios - 触摸时取消 UIButton 触摸

iphone - 为什么这张图片没有添加到我的 ScrollView 中?

iphone - iCarousel 从 NSMutableArray 加载 UIViews

ios - 为什么 UIViewController 子类的 Swift 默认初始化程序会两次初始化属性?