开发者社区> 问答> 正文

设置点击手势出现了错误

我创建了一个标签,然后在上面设置的点击,但是点击之后报错

.h文件:

@interface ViewController : UIViewController
{
    UILabel *alabel;
}
@property (strong, nonatomic) UILabel *alabel;

.m文件:

- (void)viewDidLoad
{
    [super viewDidLoad];

    alabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
    alabel.text = @"Drag me!";

    alabel.userInteractionEnabled = YES;

    UITapGestureRecognizer *tapGesture =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap)];
    [alabel addGestureRecognizer:tapGesture];

    [self.view addSubview:alabel];
}

- (void)labelTap:(UITapGestureRecognizer *)tapGesture {
    TouchLabelViewController *touchLabelViewController = [[TouchLabelViewController alloc] initWithNibName:@"TouchLabelViewController" bundle:[NSBundle mainBundle]];
    [self presentModalViewController:touchLabelViewController animated:NO];
    }

日志显示:

2012-10-29 11:19:17.313 DragableControll[795:f803] -[ViewController labelTap]: unrecognized selector sent to instance 0x68956b0
2012-10-29 11:19:17.316 DragableControll[795:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController labelTap]: unrecognized selector sent to instance 0x68956b0'

展开
收起
爵霸 2016-05-27 11:49:39 1835 0
1 条回答
写回答
取消 提交回答
  • lz把Selector加错了

     UITapGestureRecognizer *tapGesture =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap)];
    ``
    
    应该改为:

    UITapGestureRecognizer *tapGesture =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap:)];

    2019-07-17 19:17:37
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载