<bdo id='h4Ycw'></bdo><ul id='h4Ycw'></ul>
      <tfoot id='h4Ycw'></tfoot>

    1. <legend id='h4Ycw'><style id='h4Ycw'><dir id='h4Ycw'><q id='h4Ycw'></q></dir></style></legend>
      <i id='h4Ycw'><tr id='h4Ycw'><dt id='h4Ycw'><q id='h4Ycw'><span id='h4Ycw'><b id='h4Ycw'><form id='h4Ycw'><ins id='h4Ycw'></ins><ul id='h4Ycw'></ul><sub id='h4Ycw'></sub></form><legend id='h4Ycw'></legend><bdo id='h4Ycw'><pre id='h4Ycw'><center id='h4Ycw'></center></pre></bdo></b><th id='h4Ycw'></th></span></q></dt></tr></i><div id='h4Ycw'><tfoot id='h4Ycw'></tfoot><dl id='h4Ycw'><fieldset id='h4Ycw'></fieldset></dl></div>

      <small id='h4Ycw'></small><noframes id='h4Ycw'>

    2. 目标 C:从 UIButton 类的子类创建的按钮不起作用

      时间:2023-07-07

    3. <small id='2qkm6'></small><noframes id='2qkm6'>

            <legend id='2qkm6'><style id='2qkm6'><dir id='2qkm6'><q id='2qkm6'></q></dir></style></legend>
              <bdo id='2qkm6'></bdo><ul id='2qkm6'></ul>
                  <tbody id='2qkm6'></tbody>
                <tfoot id='2qkm6'></tfoot>
                <i id='2qkm6'><tr id='2qkm6'><dt id='2qkm6'><q id='2qkm6'><span id='2qkm6'><b id='2qkm6'><form id='2qkm6'><ins id='2qkm6'></ins><ul id='2qkm6'></ul><sub id='2qkm6'></sub></form><legend id='2qkm6'></legend><bdo id='2qkm6'><pre id='2qkm6'><center id='2qkm6'></center></pre></bdo></b><th id='2qkm6'></th></span></q></dt></tr></i><div id='2qkm6'><tfoot id='2qkm6'></tfoot><dl id='2qkm6'><fieldset id='2qkm6'></fieldset></dl></div>
              1. 本文介绍了目标 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 的重要之处在于它继承自 UIControl,UIControl 类参考 本身

                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:

                值得一读的参考资料

                • UIView 编程指南:视图和窗口架构 -> 有效使用视图的技巧 -> 不要通过嵌入子视图来自定义控件

                来源:我在这里发帖

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

                上一篇:在 UIButton 中使用 UIBarButtonItem 图标 下一篇:如何在两个 PNG 图像之间为 UIButton 设置动画?

                相关文章

                  <i id='YY2N9'><tr id='YY2N9'><dt id='YY2N9'><q id='YY2N9'><span id='YY2N9'><b id='YY2N9'><form id='YY2N9'><ins id='YY2N9'></ins><ul id='YY2N9'></ul><sub id='YY2N9'></sub></form><legend id='YY2N9'></legend><bdo id='YY2N9'><pre id='YY2N9'><center id='YY2N9'></center></pre></bdo></b><th id='YY2N9'></th></span></q></dt></tr></i><div id='YY2N9'><tfoot id='YY2N9'></tfoot><dl id='YY2N9'><fieldset id='YY2N9'></fieldset></dl></div>
                  <legend id='YY2N9'><style id='YY2N9'><dir id='YY2N9'><q id='YY2N9'></q></dir></style></legend>
                    <tfoot id='YY2N9'></tfoot>

                    <small id='YY2N9'></small><noframes id='YY2N9'>

                      <bdo id='YY2N9'></bdo><ul id='YY2N9'></ul>