【问题标题】:In iOS button tapping not fired when Keyboard is visible?在 iOS 中,当键盘可见时没有触发按钮点击?
【发布时间】:2016-01-11 09:09:26
【问题描述】:

我对其中的UITableViewUIButtons 有一个非常奇怪的问题。

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

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

任何想法可能是原因?

我拦截了Gesture Recognizer,但是视图的标签是0——不管我把它设置为-1300。我使用代码:

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

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

// 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);

}

【问题讨论】:

    标签: ios objective-c uitableview keyboard gesture


    【解决方案1】:

    如果视图是您的按钮,您需要实现 UIGestureRecognizerDelegate 并绕过手势识别器操作。

    - (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:

    【讨论】:

    • 这不起作用 - 无法识别点击按钮。这很烦人——有时有效,有时无效。我想键盘的显示会误导点击位置。
    • 如果按钮意味着在底部,为什么不将它们添加为节页脚或 tableviewfooter 并尝试..
    • 是的,按钮是独立的 UITableViewCell。 10x,我会试试的。
    猜你喜欢
    • 1970-01-01
    • 2018-01-05
    • 2014-06-13
    • 1970-01-01
    • 2018-06-11
    • 2020-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多