【发布时间】: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