iOS子类化UIControl没有响应
Posted
技术标签:
【中文标题】iOS子类化UIControl没有响应【英文标题】:iOS subclassing UIControl not responding 【发布时间】:2012-04-17 05:21:40 【问题描述】:我正在做一个子类化 uiControl 的测试,但它还没有工作,
这是我的代码:
MainVC.m::
- (void)viewDidLoad
[super viewDidLoad];
TesteaContol *tt = [[[TesteaContol alloc]initWithFrame:CGRectMake(20, 20, 80, 30)]autorelease];
[tt addTarget:self action:@selector(ttActiono:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:tt];
- (void)ttActiono:(TesteaContol*)message
NSLog(@"proyection");
TesteaContol.h
@interface TesteaContol : UIControl
TesteaControl.m
#import "TesteaContol.h"
@implementation TesteaContol
- (id)initWithFrame:(CGRect)frame
self = [super initWithFrame:frame];
if (self)
// Initialization code
NSLog(@"cao");
UIButton *vacas = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[vacas setTitle:@"pingus" forState:UIControlStateNormal]; ;
vacas.titleLabel.textColor = [UIColor blackColor];
vacas.frame = CGRectMake(0, 0, 80, 40);
[vacas addTarget:self action:@selector(vacasPressed:) forControlEvents:UIControlStateNormal];
[self addSubview:vacas];
return self;
- (void) vacasPressed:(id) sender
NSLog(@"vacanio");
[self sendActionsForControlEvents:UIControlEventTouchUpInside];
所以我正在使用 uiButton 进行测试,我看到了按钮,但没有响应触摸,
我错过了什么?
谢谢!
【问题讨论】:
【参考方案1】:这里有问题:
[vacas addTarget:self action:@selector(vacasPressed:)
forControlEvents:UIControlStateNormal];
“forControlEvents”部分应该是 UIControlEvents
类型,而 UIControlStateNormal
不是。对于常规按钮,您希望将 UIControlStateNormal
替换为 UIControlEventTouchUpInside
:
[vacas addTarget:self action:@selector(vacasPressed:)
forControlEvents:UIControlEventTouchUpInside];
【讨论】:
以上是关于iOS子类化UIControl没有响应的主要内容,如果未能解决你的问题,请参考以下文章
iOS:响应者辞职时,文本从子类 UITextfield 中消失