在iOS中,当键盘可见时不触发按钮点击吗? [英] In iOS button tapping not fired when Keyboard is visible?

查看:112
本文介绍了在iOS中,当键盘可见时不触发按钮点击吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在其中有UITableViewUIButtons时遇到了一个非常奇怪的问题.

I have a very strange issue with UITableView and UIButtons inside it.

我的表包含3个部分,每个部分都有1个原型单元.最后一种类型包含4个按钮(其中2个按钮会触发连接到Web服务的过程,并触发segue转到下一页).我在UITableViewCell的第一种类型中也有UITextField.

My table contains 3 sections with 1 prototype cell for each. The last type contains 4 buttons (2 of which fires procedure which connects to a web service and triggers segue to go to the next page). I also have UITextField inside first type of UITableViewCell.

除了一种情况外,我的逻辑工作正常-当我在 EDIT 中并且键盘可见时-我进行滚动(不退出编辑模式).当我按下两个按钮之一时,我仅退出 EDIT 模式-不会触发该按钮的点击.

My logic works fine except one case - when I am inside EDIT and my keyboard is visible - I make scroll (not exiting edit mode). When I press one of the two buttons I only exit EDIT mode - the tap for the button is not fired.

有什么想法可能是原因吗?

Any ideas which can be the reason?

我拦截了Gesture Recognizer,但是视图的标记为0-不管我将其设置为-1300.我使用代码:

I intercept Gesture Recognizer but the tag of the view is 0 - no matter I set it as -1300. I use code:

UIView *view = gestRecognzr.view;
NSLog(@"%d", view.tag);//By tag, you can find out where you had tapped.

我使用通知和以下代码显示键盘:

I show keyboard using notification and following code:

// Add code for keyboard management
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardShow:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardHide:)
                                             name:UIKeyboardWillHideNotification
                                           object:nil];

...和...

// ------------------------------------------------------

#pragma mark keyboard visual management
// ------------------------------------------------------

- (void) keyboardShow: (NSNotification*) n {

    if (!keyboardIsVisible){
        self->_oldContentInset = self.tvPayments.contentInset;
        self->_oldIndicatorInset = self.tvPayments.scrollIndicatorInsets;
        self->_oldOffset = self.tvPayments.contentOffset;

        NSDictionary* d = [n userInfo];
        CGRect r = [[d objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
        r = [self.tvPayments convertRect:r fromView:nil];
        CGRect f = self.fr.frame;
        CGFloat y =
        CGRectGetMaxY(f) + r.size.height -
        self.tvPayments.bounds.size.height + 5;
        if (r.origin.y < CGRectGetMaxY(f)) {
            NSNumber* duration = d[UIKeyboardAnimationDurationUserInfoKey];
            NSNumber* curve = d[UIKeyboardAnimationCurveUserInfoKey];
            [UIView animateWithDuration:duration.floatValue
                                  delay:0
                                options:curve.integerValue << 16
                             animations:^{
                                 CGRect b = self.tvPayments.bounds;
                                 b.origin = CGPointMake(0, y);
                                 self.tvPayments.bounds = b;
                             } completion: nil];
        }
        UIEdgeInsets insets = self.tvPayments.contentInset;
        insets.bottom = r.size.height;
        self.tvPayments.contentInset = insets;
        insets = self.tvPayments.scrollIndicatorInsets;
        insets.bottom = r.size.height;
        self.tvPayments.scrollIndicatorInsets = insets;

        keyboardIsVisible = YES;
    }
}

- (void) keyboardHide: (NSNotification*) n {

    if (keyboardIsVisible){
        NSNumber* duration = n.userInfo[UIKeyboardAnimationDurationUserInfoKey];
        NSNumber* curve = n.userInfo[UIKeyboardAnimationCurveUserInfoKey];
        [UIView animateWithDuration:duration.floatValue
                              delay:0
                            options:curve.integerValue << 16
                         animations:^{
                             CGRect b = self.tvPayments.bounds;
                             b.origin = self->_oldOffset;
                             self.tvPayments.bounds = b;
                             self.tvPayments.scrollIndicatorInsets = self->_oldIndicatorInset;
                             self.tvPayments.contentInset = self->_oldContentInset;

                         } completion:nil];

        keyboardIsVisible = NO;
    }
}
// ------------------------------------------------------

...和...

/* Tap recognizer */
-(void)tap:(UIGestureRecognizer *)gestRecognzr
{
    @try {

        gestRecognzr.cancelsTouchesInView = NO;

        [self.view endEditing:YES];

        UIView *view = gestRecognzr.view;
        NSLog(@"%d", view.tag);//By tag, you can find out where you had tapped.

    }
    @catch (NSException *exception) {
        [self throwUnknownException:exception];
    }
}

-(BOOL) textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];
    return YES;
}

- (BOOL)textFieldShouldBeginEditing:(PEStfCustomTextField *)textField {

    [textField startEditMode:YES];
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(PEStfCustomTextField *)textField {

    [textField endEditMode:YES];

    return YES;
}

- (BOOL)textField:(PEStfCustomTextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    return [textField cycleEditMode:YES shouldChangeCharactersInRange:range replacementString:string];
}

- (void)textFieldDidBeginEditing:(PEStfCustomTextField *)textField
{


    if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad) {

        UITextField *field = (UITextField*)textField;
        _frameY = field.frame.origin.y;
        _frameHeight = field.frame.size.height;

        UIView *separatorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), 1.0 / [UIScreen mainScreen].scale)];
        separatorView.backgroundColor = [UIColor darkGrayColor];
        textField.inputAccessoryView = separatorView;
    }

    self.fr = textField; // keep track of first responder
}

- (void)textFieldDidEndEditing:(PEStfCustomTextField *)textField {

    NSNumberFormatter *frmtr = [[NSNumberFormatter alloc] init];
    frmtr.numberStyle = NSNumberFormatterDecimalStyle;
    NSNumber *myAmount = [frmtr numberFromString:textField.text];

    NSString *currAmount = textField.text;
    int Amount;

    if(([textField.text length] != 0) && (![myAmount isEqual:@0])) {

        currAmount = [currAmount stringByReplacingOccurrencesOfString:constCharComma withString:constCharFullStop];
        Amount = [currAmount doubleValue] * 100.0;

        textField.text = [singApp formatAmount:Amount];

    }
    else
    {
        Amount = 0;

        textField.text = @"";
    }

    long section = (textField.tag - 3000) % 10000;
    long row = (textField.tag - 3000 - section) / 10000;


    [singApp.paymentInfo setItemRechargeAmount:Amount row:row section:section-1];

    UILabel *lbl = (UILabel *)[self.view viewWithTag:NSIntegerMax];

    long long bulletinsAmount;
    long long rechargesAmount = [singApp.paymentInfo getTotalRechargesAmount];

    if (singApp.paymentInfo.isBulletinPaymentDenied)
    {
        bulletinsAmount = 0;
    }
    else
    {
        bulletinsAmount = (long long)[self getBulletinsAmount];
    }

    lbl.text = [hdrTotal stringByReplacingOccurrencesOfString:@"{0}" withString:[singApp formatEuroAmount:(bulletinsAmount + rechargesAmount)]];
    isTotalZero = (bulletinsAmount + rechargesAmount == 0);

}

推荐答案

如果需要单击视图,则需要实现UIGestureRecognizerDelegate并绕过手势识别器动作.

You need to implement UIGestureRecognizerDelegate and bypass gesturerecognizer action if view is you button.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    return (touch.view != yourButton);
}

访问 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIGestureRecognizerDelegate_Protocol/index.html#//apple_ref/occ/intfm/UIGestureRecognizerDelegate/gestureRecognizer:shouldReceiveTouch :

这篇关于在iOS中,当键盘可见时不触发按钮点击吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆