我在打開代碼中的手勢識別器時遇到了麻煩。我添加了一個handleTap回調到tap手勢識別器,但是print語句從不發生。我只是沒有任何運氣。有人看到我在這裏做錯了嗎?如何使UIGestureRecognizers正常工作?
在我ViewController.h
@interface ViewController : UIViewController<UITextFieldDelegate, UIGestureRecognizerDelegate> {
}
@end
這是我在我的ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
View *myview = [[View alloc] initWithFrame:applicationFrame];
myview.userInteractionEnabled = YES;
myview.multipleTouchEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleTap)];
tap.numberOfTapsRequired = 1;
tap.delegate = self;
[myview addGestureRecognizer:tap];
self.view = myview;
}
-(void)handleTap:(UITapGestureRecognizer*)recognizer {
NSLog(@"dismiss keyboard");
//[usernameTextField resignFirstResponder];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
return YES;
}
編輯:我添加了一個文本字段,當我點擊文本字段我請參閱分路選擇器的打印聲明。但是,如果我點擊文本字段,我不會得到選擇器打印語句。
我添加了冒號,但仍然沒有運氣。 – Xavier
你檢查了UIView'myView'是否在頂部並且可見? – colincameron
我只有一個視圖,它有可見的按鈕和文本字段。 – Xavier