objective-c - Objective-C : Buttons created from subclass of UIButton class not working

标签 objective-c ios uibutton

我正在创建 UIButton 的子类,以便创建我自己的自定义按钮。我的代码如下:

//interface file (subclass of uIButton
@interface UICustomButton : UIButton 
{
    Answer *answer;
    NSString *btnType;
}

@property (nonatomic, retain) Answer *answer;
@property (nonatomic, assign) NSString *btnType;

- (id)initWithAnswer:(Answer *)ans andButtonType:(NSString *)type andFrame:(CGRect)frame; 
- (void)buttonPressed;

@end


//Implementation file (.m)
@implementation UICustomButton
@synthesize answer,btnType;

- (id)initWithAnswer:(Answer *)ans andButtonType:(NSString *)type andFrame:(CGRect)frame; 
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        self = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        self.backgroundColor = [UIColor colorWithHexString:@"#E2E4E7"];

    }

    [self addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlStateNormal];

    self.answer = ans;
    self.btnType = type;

    return self;
}

我在让上述代码工作时遇到了一些问题。我有两个问题

1) 按钮没有响应选择器方法“buttonPressed”

2) 我遇到了“self.answer = ans”和“self.btnType = type”堆栈跟踪行的运行时错误,如下所示:

-[UIButton setAnswer:]: unrecognized selector sent to instance 0x614ebc0
2011-06-23 00:55:27.038 onethingaday[97355:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton setAnswer:]: unrecognized selector sent to instance 0x614ebc0'

我在这里做错了什么?

最佳答案

发生这种情况是因为您创建的是 UIButton 类型的对象,而不是在 init 方法中创建的 UICustomButton 类型

self = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];

尝试将您的 init 方法替换为

- (id)initWithAnswer:(Answer *)ans andButtonType:(NSString *)type andFrame:(CGRect)frame; 
{
    self = [self initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    if (self) 
    {
        self.backgroundColor = [UIColor colorWithHexString:@"#E2E4E7"];

        [self addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];

        self.answer = ans;
        self.btnType = type;
    }

    return self;
}

这将导致 self 成为 UICustomButton 类型的对象。

此外,当您使用 addTarget:action:forControlEvents: 方法将目标添加到按钮时,您使用的 UIControlState 参数类型错误

您应该使用以下值:

UIControlEventTouchDown
UIControlEventTouchDownRepeat
UIControlEventTouchDragInside
UIControlEventTouchDragOutside
UIControlEventTouchDragEnter
UIControlEventTouchDragExit
UIControlEventTouchUpInside
UIControlEventTouchUpOutside
UIControlEventTouchCancel


编辑: 关于 UIButton 子类化的注意事项

网络上的许多引用资料都说你不应该将 UIButton 类子类化,但不仅有人说了为什么,而且让我非常恼火的是 UIButton Class Reference对此什么也没说。

如果你取UIWebView Class Reference例如,它明确指出你不应该继承 UIWebView

Subclassing Notes The UIWebView class should not be subclassed.

UIButton 的重要之处在于它继承自 UIControlUIControl Class Reference 上有一个很好且简单的解释本身

Subclassing Notes You may want to extend a UIControl subclass for either of two reasons:

  • To observe or modify the dispatch of action messages to targets for particular events
  • To provide custom tracking behavior (for example, to change the highlight appearance)

因此,这意味着您可以UIButton 子类化,但您应该小心您正在做的事情。只需将它子类化以改变它的行为而不是它的外观。要修改 UIButton 外观,您应该使用为此提供的接口(interface)方法,例如:

setTitle:forState:
setBackgroundImage:forState:
setImage:forState:

值得一读的引用资料

来源:my post here

关于objective-c - Objective-C : Buttons created from subclass of UIButton class not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6443639/

相关文章:

ios - 为什么调用 super.init() 会调用我默认的 init()

ios - 如何根据大小类安装和卸载 UIButton?

ios - 不使用 "hidden"或 "disabled"隐藏 UIButton

iphone - viewWillAppear : and viewDidAppear: 之间的 View 框架变化

ios - SpeechKit 转录音频文件

ios - 点语法和方括号

ios - 如何使用 UIButton 清除 UITextView

ios - 打开位置设置在 iOS 11 中不起作用

ios - NSString 到 SecKeyRef

iphone - 如果我调用此方法,则会收到错误 "database is lock"。我该如何解决这个问题?请帮我