【问题标题】:How Can I restrict the prefix textfield string in iOS?如何在 iOS 中限制前缀文本字段字符串?
【发布时间】:2015-04-17 09:28:07
【问题描述】:

如何限制textField字符串的前缀。

我想要文本字段具有默认前缀字符串(AAAA-)的效果。

(AAAA-用户添加字符串后)

并且用户不能删除这些字符串,用户可以将字符串添加到textField

如何使用下面的委托方法达到效果?

-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
 {

 }

我不想自定义模态视图来实现这些效果。

有一些textfield方法可以实现吗?

谢谢

---详细解释问题---

我使用 UIAlertAction 在 AlertAction 中添加文本字段,如下所示:

在我声明的界面

NSString *_prefixString;

在viewdidload中设置initvalue

_prefixString = @"AAAA-";

我已经在头文件中设置了 UITextFieldDelegate。

然后alertviewcontroller设置如下:

    UIAlertController *changeDeviceNameDialog =[UIAlertController
                                             alertControllerWithTitle:NSLocalizedString(@"change_device_name",nil)  message:nil preferredStyle:UIAlertControllerStyleAlert];

         UIAlertAction *cancelChangeDevNameAc = [UIAlertAction actionWithTitle:NSLocalizedString(@"cancel",nil) style:UIAlertActionStyleDefault handler:nil];
         UIAlertAction *enterChangeDevNameAc = [UIAlertAction actionWithTitle:NSLocalizedString(@"enter",nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){

        UITextField *newDeviceNameTF = changeDeviceNameDialog.textFields[0];
      }];

    [changeDeviceNameDialog addAction:cancelChangeDevNameAc];
    [changeDeviceNameDialog addAction:enterChangeDevNameAc];
    [changeDeviceNameDialog addTextFieldWithConfigurationHandler:^(UITextField *textField){
        textField.delegate = self;
        textField.placeholder = NSLocalizedString(@"new_device_name", nil);
        textField.text = _prefixString;
        }];
       [self presentViewController:changeDeviceNameDialog animated:YES completion:nil];

// below refer the replay

     -(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
     {
      if ([textField.text isEqualToString:_prefixString] && range.location == _prefixString.length - 1 && range.length == 1) {
              return NO;
         }
      return YES;
     }

我想要的效果是当警报弹出时,文本字段有默认文本 AAAA-,并且用户无法清除 AAAA- 文本。

用户只需输入其他文本附加 AAAA-。

但是像upper这样的委托,我还是可以去掉前缀文字的。

【问题讨论】:

标签: ios delegates uitextfield uialertcontroller


【解决方案1】:

试试这个,

设置您的prefixString

self.prefixString = @"PREFIX";

然后在delegate方法中,

-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

  if ([textField.text isEqualToString:_prefixString] && range.location == _prefixString.length - 1 && range.length == 1) {

      return NO;
  }

  return YES;
}

【讨论】:

  • if ([textField.text isEqualToString:_prefixString] && range.location == _prefixString.length - 1 && range.length == 1) { textField.text = @"AAAA-" 返回 NO;该方法仍然可以按删除键远程前缀字符串。@@我想始终显示 AAAA-,用户可以将字符串添加到 AAAA-whatever 。不能 AAAAxxx。
  • @dickfala 为什么要在委托中设置 textField 文本?对我来说,它就像你说的那样工作。
【解决方案2】:

如果replacementString的长度为-1,并且是你希望防止被删除的字符串的范围,则返回FALSE。

【讨论】:

  • 是这些吗? if(string.length
  • 不完全是,replacementString 应该为您提供用户在键盘上按下的单个字符串。所以,if(string.length < 0) {return false;}
【解决方案3】:

您可以按照下面的代码设置textFieldsetLeftViewMode 来实现您想要的。

UILabel *lblPrifix = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
lblPrifix.text = @"AAAA-";
[_textField setLeftView:poundLabel];
[_ textField setLeftViewMode:UITextFieldViewModeAlways];

希望对您有所帮助。

【讨论】:

  • 嗨,我使用 UIAlertController。所以我不能在 UIAlertController 中添加 UILabel。您可以在我编辑后参考我的问题。谢谢。
猜你喜欢
  • 1970-01-01
  • 2022-12-12
  • 1970-01-01
  • 2021-01-21
  • 1970-01-01
  • 1970-01-01
  • 2014-04-30
  • 1970-01-01
  • 2019-02-25
相关资源
最近更新 更多