[英]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.