目标 C:从 UIButton 类的子类创建的按钮不起作用 [英] objective C: Buttons created from subclass of UIButton class not working

查看:14
本文介绍了目标 C:从 UIButton 类的子类创建的按钮不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am creating a subclass of UIButton in order to create my own customized buttons. My code as follows:

//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;
}

我在使上述代码正常工作时遇到了一些问题.我有两个问题

I am facing some issues in getting the above code to work. I have 2 problems

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

1) The buttons are not responding to the selector method "buttonPressed"

2) 我在 'self.answer = ans' 和 'self.btnType = type' 行中遇到运行时错误堆栈跟踪如下:

2) I am hitting a runtime error for the lines 'self.answer = ans' and 'self.btnType = type' Stack trace as follows:

-[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'

我在这里做错了什么?

推荐答案

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

This is happening because you are creating a UIButton type object and not a UICustomButton type inside the init method when you do

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

尝试将您的 init 方法替换为

Try replacing your init method for

- (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 类型的对象.

This will cause self to be a UICustomButton type object.

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

Also, you are using a wrong type for the UIControlState parameter when you add the target to your button using the addTarget:action:forControlEvents: method

你应该使用下面的值:

UIControlEventTouchDown
UIControlEventTouchDownRepeat
UIControlEventTouchDragInside
UIControlEventTouchDragOutside
UIControlEventTouchDragEnter
UIControlEventTouchDragExit
UIControlEventTouchUpInside
UIControlEventTouchUpOutside
UIControlEventTouchCancel

UIControlEventTouchDown
UIControlEventTouchDownRepeat
UIControlEventTouchDragInside
UIControlEventTouchDragOutside
UIControlEventTouchDragEnter
UIControlEventTouchDragExit
UIControlEventTouchUpInside
UIControlEventTouchUpOutside
UIControlEventTouchCancel

<小时>

UIButton 子类化注意事项

网络上的许多参考资料都说你不应该继承 UIButton 类,但不仅有人说为什么,而且让我深感恼火的是 UIButton 类参考 根本没有说明.

Many references on the web say you should NOT subclass the UIButton class, but not only anybody said why but what also deeply annoyed me was that the UIButton Class Reference does not say anything about it at all.

如果您采取 UIWebView 类参考 例如,它明确指出你不应该继承 UIWebView

If you take UIWebView Class Reference for example, it explicitly states that you should not subclass UIWebView

子类化注释 UIWebView 类不应该被子类化.

Subclassing Notes The UIWebView class should not be subclassed.

UIButton 的重要之处在于它继承自 UIControlUIControl 类参考 本身

the big deal with UIButton is that it inherits from UIControl and a good and simple explanation is on the UIControl Class Reference itself

子类化注释你可能想要为任一扩展 UIControl 子类有两个原因:

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

  • 观察或修改调度给目标的行动信息特殊事件
  • 提供定制跟踪行为(例如,更改突出显示的外观)

所以,这意味着你可以子类化一个UIButton,但你应该小心你在做什么.只需将其子类化以更改其行为而不是外观.要修改 UIButton 外观,您应该使用为此提供的接口方法,例如:

So, this means that you CAN subclass a UIButton, but you should be careful on what you are doing. Just subclass it to change its behavior and not its appearance. To modify a UIButton appearance you should use the interface methods provided for that, such as:

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

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

值得一读的参考资料

来源:我在这里发帖

这篇关于目标 C:从 UIButton 类的子类创建的按钮不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆