我正在尝试限制允许用户在UITextField
内输入UIAlertView
字符的字符数。
我在网上尝试了一些常见的答案。如下图所示:
#define MAXLENGTH 10
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([textField.text length] > MAXLENGTH) {
textField.text = [textField.text substringToIndex:MAXLENGTH-1];
return NO;
}
return YES;
}
任何人都可以提供帮助吗?
答案 0 :(得分:7)
.h文件
@property (strong,nonatomic) UITextField *alertText;
@interface LocationSearchViewController : UIViewController<UITextFieldDelegate>
.m文件
- (IBAction)butPostalCode:(id)sender {
UIAlertView *alrt=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
alrt.alertViewStyle = UIAlertViewStylePlainTextInput;
[[alrt textFieldAtIndex:0] setPlaceholder:@"Enter postal Code"];
alertText = [alrt textFieldAtIndex:0];
alertText.keyboardType = UIKeyboardTypeNumberPad;
alertText.delegate=self;
alrt.tag=100;
[alrt show];
}
-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(yourtextfieldname.text.length >= 10 && range.length == 0) {
return NO;
}
return YES;
}
<强>夫特强>
class LocationSearchViewController: UIViewController, UITextFieldDelegate {
Var alertText:UITextField!
}
func butPostalCode(sender: AnyObject) {
var alrt: UIAlertView = UIAlertView(title: "", message: "", delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "OK")
alrt.alertViewStyle = .PlainTextInput
alrt.textFieldAtIndex(0).placeholder = "Enter postal Code"
alertText = alrt.textFieldAtIndex(0)
alertText.keyboardType = .NumberPad
alertText.delegate = self
alrt.tag = 100
alrt.show()
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if yourtextfieldname.text.length >= 10 && range.length == 0 {
return false
}
return true
}
答案 1 :(得分:1)
不幸的是,这是不允许的。您不能混淆UIAlertView
的层次结构,因此您无法像UITextField
的属性那样更改内容。
请参阅此link
具体地
UIAlertView类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改
修改强>
虽然您不应该像我上面所说的那样弄乱UIAlertView
层次结构,但您实际上可以设置UITextField
委托,但首先需要检索它。首先确保已在头文件(UITextFieldDelegate
文件)中设置.h
。
然后,在您创建UIAlertView
并设置alertViewStyle:
property后,我们就像
// We create an instance of our UIAlertView
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"My Alert"
message:nil
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
// We set the alertViewStyle to a plain text input, so 1 textfield
[myAlert setAlertViewStyle:UIAlertViewStylePlainTextInput];
// Then we retrieve that UITextField from the index 0
UITextField *plainTextField = [myAlert textFieldAtIndex:0];
// Now that we have retrieved the text field we can set the delegate on it/
// Probably best to give it a tag as well so we can identify it later.
[plainTextField setDelegate:self];
[plainTextField setTag:1001];
我们设置了委托后,您应该输入您的方法,因为shouldChangeCharactersInRange:
是UITextFieldDelegate
方法。
-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
// Make sure that it is our textField that we are working on otherwise forget the whole thing.
if([textField tag] == 1001) {
if([textField text].length >= 10 && range.length == 0) {
return NO;
}
}
return YES;
}
警告
由于从textFieldAtIndex:
检索的文本字段是UIAlertView
层次结构的一部分,Apple可能会将其归类为更改UIAlertView
不允许的内容(参见第一部分)回答)以及在使用addSubview:
时不使用UIAlertView
的任何内容
答案 2 :(得分:0)
试试这个:
#define MAXLENGTH 10
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *nextValue = [NSString stringWithFormat:@"%@%@", textField.text, string];
if ([nextValue length] > MAXLENGTH) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Attention"
message:@"You can't insert more than 10 characters"
delegate:nil cancelButtonTitle:nil
otherButtonTitles:@"Close", nil];
[alert show];
return NO;
}
return YES;
}