【问题标题】:How to add a instance variable and use it in a custom UIButton如何添加实例变量并在自定义 UIButton 中使用它
【发布时间】:2011-02-11 12:57:28
【问题描述】:

我创建了一个这样的自定义 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";

错误信息是:

* 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“* -[UIButton setSecondLine:]:无法识别的选择器已发送到实例 0x43280e0”

必须做些什么来解决这个问题?实例变量应该如何添加?

【问题讨论】:

  • rightButton.secondLine = @"你好"。对不起大家。那是一个拼写错误。我现在将更新问题。

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


【解决方案1】:

试试

rightButton.secondLine = @"hello";

你应该重写这个类方法

+ (id)buttonWithType:(UIButtonType)buttonType

返回自定义按钮的实例

【讨论】:

  • + (id)buttonWithType:(UIButtonType)buttonType { return (CustomButton*)[super buttonWithType:buttonType];我这样覆盖。我得到了错误。
  • 铸造不会这样做,因为您实际上并没有创建自定义按钮的实例。您应该自己重新创建详细信息披露按钮,或者不将该按钮子类化并通过现有方法/属性修改UIButton
【解决方案2】:

好吧,如果这就是您的 CustomButton 实现中的全部内容,那么似乎 String 没有被放入 Button 中。你是如何布置按钮的子视图的?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-16
    • 2013-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多