【问题标题】:textFieldShouldBeginEditing is called repeatedly only in iPad仅在 iPad 中重复调用 textFieldShouldBeginEditing
【发布时间】:2015-08-27 10:57:55
【问题描述】:

我有两个文本字段(姓名和电话号码)。一旦用户注册,用户应该无法编辑电话号码文本字段。当用户单击电话号码文本字段时,我正在检查条件。如果已注册,我正在创建警告说“无法编辑”。当我单击“确定”时,我关闭了键盘。我正在使用以下代码来这样做

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
 if(registered)
 {
   dismissAlert = [[UIAlertView alloc]initWithTitle:@"cannot edit" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
   [dismissAlert show];
 }
}

if([title isEqualToString:@"OK"])
{
   [self.view endEditing:YES];
   [self.PhoneNumber resignFirstResponder];
   dismissAlert = nil;
}

但是 textFieldShouldBeginEditing 被调用了多次。它只发生在 iPad 上。

【问题讨论】:

  • 你是在给[self.PhoneNumber becomeFirstResponder];打电话的地方打电话吗?
  • 不,我没有打电话给 self.buddyPhoneNumber。
  • 你为什么打电话给endEditing和resignFirstResponder?删除这些行,它将起作用。
  • @Fabio Berger 如果我没有调用 endEditing 和 resignFirstResponder,则会出现小键盘。
  • @elavarasan 好吧,您可能想展示它,不是吗?但是如果你想隐藏键盘,只需return NO;而不是endEditing和resignFirstResponder,键盘就不会出现

标签: ios objective-c ipad uitextfield


【解决方案1】:

试试这个 -

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if(textField == txtPhone)
    {
         [self performSelector:@selector(phoneAlert) withObject:nil afterDelay:0.1];
         return NO;
    }
    return YES;
}

-(void)phoneAlert
{
     dismissAlert = [[UIAlertView alloc]initWithTitle:@"cannot edit" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
     [dismissAlert show];
}

【讨论】:

    【解决方案2】:

    终于,我明白了。我设置了

    self.PhoneNumber.userInteractionEnabled = NO;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多