在键盘可见时,iOS按钮点击不会被触发?

时间:2015-10-13 14:43:44

标签: ios objective-c uitableview keyboard gesture

我有一个非常奇怪的问题,其中包含UITableViewUIButtons

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

我的逻辑工作正常,除了一个案例 - 当我在 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);

}

1 个答案:

答案 0 :(得分: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