我已经子类UIButton来绘制图形,但是"addTarget“不能工作。
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents我对这种方法没有做任何调整。
按钮图形工作在“触摸”上正常。
如果我使用一个标准的UIButton,那么"addTarget“就能正常工作。
不知道我错过了什么??
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发布于 2012-03-06 15:06:06
当您处理完事件时,您必须从事件处理程序中调用超级程序。否则按钮怎么会知道你的动作被触动了呢?
例如:
- (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
}尽管如此,你在最初的帖子上的评论是正确的,子类UIButton可能会变得很棘手。但看起来,事情似乎已经得到了控制(大部分)。祝好运!
https://stackoverflow.com/questions/9577767
复制相似问题