0

ヘッダーファイルでこれを宣言しました:

UITapGestureRecognizer* tap;

そして上viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    tap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                  action:@selector(hideKeyboard)];
    tap.enabled = NO;
    [self.view addGestureRecognizer:tap];
}

を追加UITextFieldDelegateし、これを追加しました:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    tap.enabled = YES;

    return YES;
}

鍵盤以外を触ると消えますが、鍵盤を触ってUIButtonも消えません。

なぜなのかご存知ですか?

4

3 に答える 3

0

ユーザーがボタンをタッチダウンしたときのターゲットとアクションのペアを追加します。

[button addTarget:self
           action:@selector(dismissKeyboard)
 forControlEvents:UIControlEventTouchDown];
于 2013-09-30T08:44:16.157 に答える
0
 UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
    tapGestureRecognizer.delegate = self;
    tapGestureRecognizer.numberOfTapsRequired = 1;
    [self.view addGestureRecognizer:tapGestureRecognizer];


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {


    if (touch.view == self.button) {//change it to your condition UIButton
        return NO;
    }
    return YES;
}

-(void)dismissKeyboard
{
    [self.textField resignFirstResponder];
}
于 2013-09-30T08:37:48.030 に答える