【问题标题】:How to limit text length for a textfield in a UIAlert iOS. [duplicate]如何限制 UIAlert iOS 中文本字段的文本长度。 [复制]
【发布时间】:2014-04-30 06:50:56
【问题描述】:

如何限制可以使用现有代码在 UIAlertView 的文本字段中输入的文本数量?

我是 iOS 应用开发的新手。

我的代码如下:

if(indexPath.row== 1){
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UIAlertView *alertView = [[UIAlertView alloc]
                              initWithTitle:@"Enter Novena Day"
                              message:@"Please enter the day of Novena:"
                              delegate:self
                              cancelButtonTitle:@"Cancel"
                              otherButtonTitles:@"Ok", nil];
    [alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
    UITextField *textField = [alertView textFieldAtIndex:0];
    textField.keyboardType = UIKeyboardTypeNumberPad;

    [alertView show];

}

【问题讨论】:

  • 实现文本字段的委托,并实现检查有多少字符并禁止超过允许的代码。
  • 您可以像对任何其他文本字段一样限制它。
  • 那么我可以对文本字段使用典型的委托吗?
  • @LeoNatan 真的吗?不要仅仅因为使用了错误的标签就对问题投反对票,尤其是对于新用户。出于适当的原因投反对票,例如明显缺乏首先研究答案或未能提供足够的细节来提供帮助。
  • 这是委托文本字段的正确方法吗 [textField setDelegate:shouldChangeCharactersInRange];

标签: ios objective-c uitextfield uialertview


【解决方案1】:

当你初始化警报视图时:

[[alertView textFieldAtIndex:0] setDelegate:self];

现在,self 这是您的视图控制器。所以你需要在其声明中添加<UITextFieldDelegate>

现在实现委托方法:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSUInteger newLength = [textField.text length] + [string length] - range.length;
    return (newLength > self.maxAlertTextFieldLength) ? NO : YES;
}

这取自this answer,cmets 中的链接答案。

【讨论】:

  • 出现一个问题,在 ViewController 类型的对象上找不到 maxAlertTextFieldLength。
  • @JohnDoherty 找不到,因为您需要定义一个属性并将其设置为您想要允许的字符数。我的代码就是一个例子。
【解决方案2】:

我建议更好地使用通知

检查一下

UITextField *textField = [alertView textFieldAtIndex:0];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldMaxLimit:) name:UITextFieldTextDidChangeNotification object:textField];


-(void)textFieldMaxLimit:(NSNotification*)notification
{
  UITextField *textField=(UITextField*)notification.object;

        if ([[textField text] length]>22) //choose your limit for characters
        {
            NSString *lastString=[[textField text] substringToIndex:[[textField text] length] - 1];;
            [textField setText:lastString];
        }
 } 

【讨论】:

  • 这种方法远非理想,如果用户粘贴一堆文本实际上不起作用(因为您一次假设一个字符)。最好是完全防止添加文本,然后在事后对其进行调整。
【解决方案3】:

实现 shouldChangeCharactersInRange delegate ,检查您的条件并返回所需的值。

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

  BOOL isValid = YES;

  @try
  {
     if(5 < [textField.text length])
     {
        isValid = NO;
     }
  }
  @catch (NSException *exception)
  {
     NSLog(@"%s\n exception: Name- %@ Reason->%@", __PRETTY_FUNCTION__,[exception name],[exception reason]);
  }
  @finally
  {
     return isValid;
  }
}

【讨论】:

  • 我不确定我的委派是否正确,你能检查一下` [textField setDelegate:shouldChangeCharactersInRange];`
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-08
  • 1970-01-01
  • 1970-01-01
  • 2013-10-23
  • 2015-09-13
相关资源
最近更新 更多