0

我已经创建了一个标签并在其上设置了点击手势,但是当我点击它时它会显示错误。

我的代码是 .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'
4

4 回答 4

1

查看您的代码,您错误地将选择器添加到点击手势

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

你应该这样做

   UITapGestureRecognizer *tapGesture =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap:)];
于 2012-10-29T05:58:02.580 回答
0

selector的添加不正确。您忘记了需要提供参数的选择器方法:最后即(labelTap :)

编辑:用我的替换你的线

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

UIGestureDelegate在 .h 文件中注册

于 2012-10-29T05:55:57.183 回答
0

替换下面的代码

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

注意:在labelTab附近添加冒号即(labelTap :)

于 2012-10-29T05:59:24.250 回答
0

在此处更改代码

TapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap:)];

你已经设置了选择器(labeltap)但它应该是(labeltap :)

于 2012-10-29T06:06:07.150 回答