ios - 如何添加实例变量并在自定义 UIButton 中使用它

标签 ios objective-c uibutton iphone-sdk-3.0

我像这样创建了一个自定义 UIButton

@interface CustomButton : UIButton {

    NSString *firstLine;
    NSString *secondLine;
}
@property (nonatomic, retain) NSString *firstLine;
@property (nonatomic, retain) NSString *secondLine;

@end

CustomButton* rightButton = [CustomButton buttonWithType:UIButtonTypeDetailDisclosure];
rightButton.secondLine=@"hello";

错误信息是:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[UIButton setSecondLine:]: unrecognized selector sent to instance 0x43280e0'

要解决这个问题需要做什么?实例变量应该如何添加?

最佳答案

尝试

rightButton.secondLine = @"hello";

你应该覆盖这个类的方法

+ (id)buttonWithType:(UIButtonType)buttonType

返回自定义按钮的实例

关于ios - 如何添加实例变量并在自定义 UIButton 中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2698949/

相关文章:

objective-c - 如何在进入前景时保留 UIScrollView 的框架?

ios - UINavigationController 在 swift 3 中为 nil

ios - Sprite Kit - 如果球正在远离或靠近地面,则由于脉冲和重力检测 Y

iphone - UIButton 选定状态在 iOS 7 中不起作用

ios - 带有 UIButton 作为自定义 View 的 UIBarButtonItem 不显示

ios - SKLightNode 不使用 SKCameraNode 缩放

javascript - Node.Js 是否与应用程序的 iPhone Objective C 开发兼容?

ios - 使用 NSNotificationCenter 在 VC 之间发送数据

ios - 从 UIButton 而不是 UIBarButtonItem 显示 WEPopover

ios - 切换屏幕时存储数据