在键盘可见时,iOS按钮点击不会被触发?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在键盘可见时,iOS按钮点击不会被触发?相关的知识,希望对你有一定的参考价值。
我有一个非常奇怪的问题,其中有UITableView
和UIButtons
。
我的表包含3个部分,每个部分有1个原型单元格。最后一个类型包含4个按钮(其中2个触发连接到Web服务并触发segue进入下一页的过程)。我也有第一类UITextField
内的UITableViewCell
。
我的逻辑工作正常,除了一个案例 - 当我在编辑内部并且我的键盘可见时 - 我进行滚动(不退出编辑模式)。当我按下两个按钮之一时,我只退出编辑模式 - 按钮没有被触发。
任何想法可能是什么原因?
我拦截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);
}
答案
如果您是按钮,则需要实现UIGestureRecognizerDelegate并绕过手势识别器操作。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
return (touch.view != yourButton);
}
以上是关于在键盘可见时,iOS按钮点击不会被触发?的主要内容,如果未能解决你的问题,请参考以下文章
当键盘可见时,离子页脚不会被向上推(ios 和 android)