首页>Program>source

我正在创建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'

我在这里做错什么了?

最新回答
  • 11天前
    1 #

    这是因为您正在创建 UIButton 键入对象而不是 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


    编辑: Notes on UIButton subclassing

    网络上的许多参考都说您不应该将 UIButton子类化 类,但不仅有人说了为什么,而且令我非常恼火的是,UIButton类参考根本没有讲任何关于它的内容。

    如果以UIwebView类引用为例,它明确指出您不应将 UIWebView子类化

    Subclassing Notes The UIWebView class should not be subclassed.

    UIButton的大交易 是它从 UIControl继承而来 关于UIControl类参考本身,有一个很好而简单的解释

    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)

    所以,这意味着您 CAN 子类 ,但您在执行操作时应格外小心.只需将其子类化即可更改其 UIButtonbehavior 它的 not .修改 appearance 外观,您应该使用为此提供的接口方法,例如:

    UIButton

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

    References worth reading

    来源:我在这里的帖子

  • 11天前
    2 #

    不确定之前是否在文档中,但是无论如何,这些是

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

    上的最新注释 ...

    在我看来,只要您使用 + (id)buttonWithType:(UIButtonType)buttonType,就可以进行子类化 代替 init .我还没有亲自尝试。

    buttonWithType pre

    Discussion This method is a convenience constructor for creating button objects with specific configurations. It you subclass UIButton, this method does not return an instance of your subclass. If you want to create an instance of a specific subclass, you must alloc/init the button directly.

    When creating a custom button—that is a button with the type UIButtonTypeCustom—the frame of the button is set to (0, 0, 0, 0) initially. Before adding the button to your interface, you should update the frame to a more appropriate value.

  • 11天前
    3 #

    如果您想在用户与您的按钮进行交互时获得通知,只需sublcass UIButton并实现以下方法:

    //
    //  BtnClass.m
    #import "BtnClass.h"
    @implementation BtnClass
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code
    
        }
        return self;
    }
    //added custum properities to button
    -(id)initWithCoder:(NSCoder *)aDecoder
    {
        NSLog(@"initWithCoder");
        self = [super initWithCoder: aDecoder];
        if (self) {
            // Initialization code
            _numberOfItems=[[UILabel alloc]initWithFrame:CGRectMake(40, 8, 160, 30)];
            _numberOfItems.textAlignment=NSTextAlignmentLeft;
            _numberOfItems.font = [UIFont boldSystemFontOfSize:18.0];
            _numberOfItems.textColor = [UIColor darkGrayColor];
            [self addSubview:_numberOfItems];
            _leftImage=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 25, 25)];
            [self addSubview:_leftImage];
            _rightImage=[[UIImageView alloc]initWithFrame:CGRectMake(280, 10, 15, 15)];
            [self addSubview:_rightImage];
            [self setImage:[UIImage imageNamed:@"list-bg2-1.png"] forState:UIControlStateNormal];
            [_rightImage setImage:[UIImage imageNamed:@"carat.png"]];
            self.backgroundColor=[UIColor blackColor];
    
            if(self.tag==1)
            {
                [_leftImage setImage:[UIImage imageNamed:@"notes-icon.png"]];
            }
            if(self.tag==2)
            {
                [_leftImage setImage:[UIImage imageNamed:@"photos-icon.png"]];
            }
            if(self.tag==3)
            {
                [_leftImage setImage:[UIImage imageNamed:@"videos-icon.png"]];
            }
    
        }
        return self;
    }
    //selected method of uibutton
    -(void)setSelected:(BOOL)selected
    {
        [super setSelected:selected];
    
        if(selected)
        {
            [self setImage:nil forState:UIControlStateNormal];
            _numberOfItems.textColor = [UIColor whiteColor];
            [_rightImage setImage:[UIImage imageNamed:@"carat-open.png"]];
            if(self.tag==1)
            {
                [_leftImage setImage:[UIImage imageNamed:@"white-notes-icon.png"]];
            }
            else if(self.tag==2)
            {
                [_leftImage setImage:[UIImage imageNamed:@"white-photo-icon.png"]];
            }
            else
            {
                [_leftImage setImage:[UIImage imageNamed:@"white-video-icon.png"]];
            }
        }
        else{
            _numberOfItems.textColor = [UIColor darkGrayColor];
            if(self.tag==1)
            {
                [_leftImage setImage:[UIImage imageNamed:@"notes-icon.png"]];
            }
            if(self.tag==2)
            {
                [_leftImage setImage:[UIImage imageNamed:@"photos-icon.png"]];
            }
            if(self.tag==3)
            {
                [_leftImage setImage:[UIImage imageNamed:@"videos-icon.png"]];
            }
            [self setImage:[UIImage imageNamed:@"list-bg2-1.png"] forState:UIControlStateNormal];
            [_rightImage setImage:[UIImage imageNamed:@"carat.png"]];
        }
    }
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect
    {
        // Drawing code
    }
    */
    @end
    

    不需要初始化方法。

  • 11天前
    4 #

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"touchesBegan"); } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"touchesEnded"); } - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"touchesCancelled"); } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"touchesMoved"); }

    这个答案可以追溯到几年前,而且情况已经发生了变化-正如Apple文档现在明确提到子类化并给出了一些提示。

    因此,以下答案可能是 Edit 如果您对当前的技术水平感兴趣,可能会被忽略。


    UIButton是 irrelevant or wrong for current development 打算被子类化。

    最好创建一个类别并定义一个提供所需按钮的工厂方法(适当调用 not ). buttonWithType: 仍然不是初始化按钮的正确方法。

    initWithFrame:

  • java:ServletResponse和HttpServletResponsewrapper之间的区别?
  • 如何在某些页面上强制显示离子后退按钮?