如何限制UIAlertView UITextField中的字符输入

我试图限制我的用户可以在我的UIAlertView输入我的UITextField的字符数。

我在网上尝试了一些常见的答案。 如下图所示:

  #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; } 

有人能帮忙吗?

.h文件

  @property (strong,nonatomic) UITextField *alertText; @interface LocationSearchViewController : UIViewController 

.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 } 

不幸的是,这是不允许的。 您不能使用UIAlertView的层次结构,因此您不能像UITextField的属性那样更改它。

请看这个链接

特别

UIAlertView类旨在按原样使用,不支持子类化。 此类的视图层次结构是私有的,不得修改

编辑

虽然您不应该像我上面所说的那样弄乱UIAlertView层次结构,但实际上您可以设置UITextField的委托,但首先需要检索它。 首先确保在头文件( .h文件)中设置了UITextFieldDelegate

然后在创建了UIAlertView并设置alertViewStyle:属性之后 ,我们就有类似的东西了

 // 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

试试这个:

 #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; }