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

查看:92
本文介绍了objective 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;
}

我在使上述代码工作时面临一些问题。我有2个问题

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'

我做错了什么在这里?

推荐答案

这种情况正在发生,因为您正在创建一个 UIButton 类型当你执行

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 方法

- (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.

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

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

你应该在其中使用值bellow:

You should use value among the ones bellow:


UIControlEventTouchDown

UIControlEventTouchDownRepeat

UIControlEventTouchDragInside

UIControlEventTouchDragOutside < br>
UIControlEventTouchDragEnter

UIControlEventTouchDragExit

UIControlEventTouchUpInside

UIControlEventTouchUpOutside

UIControlEventTouchCancel

UIControlEventTouchDown
UIControlEventTouchDownRepeat
UIControlEventTouchDragInside
UIControlEventTouchDragOutside
UIControlEventTouchDragEnter
UIControlEventTouchDragExit
UIControlEventTouchUpInside
UIControlEventTouchUpOutside
UIControlEventTouchCancel

编辑:
关于UIButton子类化的说明

网上很多引用都说你不应该继承 UIButton 类,但不仅仅是有人说过w hy,但也让我深感不安的是 UIButton Class Reference 根本没有说明任何内容。

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 的重大关系是它继承自 UIControl 并且 UIControl类Refe rence 本身

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子类,用于
of two原因:

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


  • 观察或修改向
    特定事件的目标发送
    动作消息

  • 提供自定义
    跟踪行为(例如,
    更改突出显示)

所以,这意味着你 CAN 继承了 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:

值得一读的参考资料

  • The UIView Programming Guide: View and Window Architecture -> Tips for Using Views Effectively -> Do Not Customize Controls by Embedding Subviews

来源:我的帖子

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

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