lianlianbushell
lianlianbushell
2012-10-29 06:21
采纳率: 73.8%
浏览 3.2k
已采纳

设置点击手势出现了错误

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

.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'
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • lanhailvcao
    lanhailvcao 2012-10-29 07:57
    已采纳

    lz把Selector加错了

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

    应该改为:

    UITapGestureRecognizer *tapGesture =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap:)];
    
    点赞 评论
  • MrsSeven7
    MrsSeven7 2012-10-29 06:45

    看上去是你的selector出问题了,

    把代码改成我的:

    UITapGestureRecognizer *tapGesture =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap:)];
    
    点赞 评论
  • wsmhyxw
    wsmhyxw 2016-05-06 09:14

    好人一生平安!!!!!!!果真是这样!!!!!我是添加了一个键盘消失的事件,还以为键盘消失后,是坐标出了问题...........

    点赞 评论

相关推荐