程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> ios-設置點擊手勢出現了錯誤

ios-設置點擊手勢出現了錯誤

編輯:編程綜合問答
設置點擊手勢出現了錯誤

我創建了一個標簽,然後在上面設置的點擊,但是點擊之後報錯

.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'

最佳回答:


lz把Selector加錯了

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

應該改為:

UITapGestureRecognizer *tapGesture =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap:)];
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved