简体   繁体   English

SubClassing UIButton但我不能让“addTarget:action:forControlEvents:”执行?

[英]SubClassing UIButton but I can't get “addTarget:action:forControlEvents:” to execute?

I have subclassed UIButton to draw graphics, but the "addTarget" won't work. 我已经将UIButton子类化为绘制图形,但“addTarget”将无效。

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents

I did not do anything with making adjustments to that method. 我没有做任何调整该方法的事情。

The Button graphics work ok on the "touches". Button图形在“触摸”上工作正常。

If I use a standard UIButton then the "addTarget" works fine. 如果我使用标准的UIButton,那么“addTarget”工作正常。

Not sure what I am missing?? 不知道我错过了什么?

thx 谢谢

#import <UIKit/UIKit.h>

@interface CButton : UIButton
{
    CGContextRef c;
    int myState;
}
@end

#import "CButton.h"

@implementation CButton

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    //NSLog(@"init state: %d", self.state);
    return self;
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
  //  myState = 1;
    myState = !myState;
   // NSLog(@"BeginState: %d", self.state);

    [self setNeedsDisplay];
}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
  //  myState = 0;
  //  NSLog(@"EndState: %d", self.state);
  //  [self setNeedsDisplay];

}

- (void)drawRect:(CGRect)rect
{
    // Drawing code

}
@end

You have to call super from your event handlers when you're all done handling events. 当你完成处理事件时,你必须从你的事件处理程序调用super。 How else will the button know of the touches to call your action? 该按钮如何知道触动您的动作?

eg 例如

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
  //  myState = 1;
    myState = !myState;
   // NSLog(@"BeginState: %d", self.state);

    [self setNeedsDisplay];

    [super touchesBegan:touches withEvent:event]; // you need this
}

That being said, the comments on your original post are right, subclassing UIButton can get tricky. 话虽如此,你原来的帖子上的评论是正确的,UIButton的子类化可能会变得棘手。 But it looks as if you have things under control (mostly). 但看起来好像你控制的东西(主要是)。 Good luck! 祝好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UIButton addTarget:action:ForControlEvents无法正常工作 - UIButton addTarget:action:ForControlEvents not working UIButton的替代选项addTarget:action:forControlEvents:IOS中的方法 - Alternative option of UIButton addTarget:action:forControlEvents: method in IOS addTarget:action:forControlEvents:无法识别的选择器发送到实例-uitablecell中的uibutton - addTarget:action:forControlEvents: unrecognized selector sent to instance - uibutton in uitablecell UIButton addTarget:action:forControlEvents:不能将自己用作目标吗? - UIButton addTarget:action:forControlEvents: not working using itself as a target? 如何将更多参数添加到由addTarget:action:forControlEvents:调用的选择器? - How can I add more parameters to the selector called by addTarget:action:forControlEvents:? 将参数传递给 addTarget:action:forControlEvents - Passing parameters to addTarget:action:forControlEvents 方法addTarget:action:forControlEvents:如何编程? - How is the method addTarget:action:forControlEvents: programmed? 使用addTarget获取运行时异常:Action:forControlEvents - Getting an runtime exception using addTarget:Action:forControlEvents 使用 addTarget:action:forControlEvents: 方法更改 UIImage? - Use addTarget:action:forControlEvents: method to change a UIImage? 对于addTarget:action:forControlEvents的addTarget指针行为感到困惑: - Confused about addTarget pointer behavior for addTarget:action:forControlEvents:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM