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

Posted

技术标签:

【中文标题】如何限制 UIAlertView UITextField 中的字符输入【英文标题】:How to limit character input in UIAlertView UITextField 【发布时间】:2014-06-18 09:10:32 【问题描述】:

我正在尝试限制允许我的用户在我的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;

有人可以帮忙吗?

【问题讨论】:

不幸的是,我不相信这是允许的——也指developer.apple.com/library/ios/documentation/uikit/reference/…"The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified" 你在使用 textfielddelegate 吗? 【参考方案1】:

.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

【讨论】:

【参考方案2】:

很遗憾,这是不允许的。你不能乱用UIAlertViews 层次结构,所以你不能像UITextFields 属性那样去改变它。

请看这个link

具体

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

编辑

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

然后在你创建了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: 检索的文本字段是UIAlertViews 层次结构的一部分,Apple 可能会将其归类为更改不允许的UIAlertView(请参阅答案的第一部分)以及您所做的任何事情。使用UIAlertViews时不要使用addSubview:

【讨论】:

@rosshump 请注意,自从我写这篇文章以来,该文档实际上已被更改,尽管它仍然符合我所说的那样,它围绕更改 UIAlertView 层次结构的文档较少,因为您 @987654341 @ 已被弃用。 哦,好的,谢谢你告诉我!我还没有为 iOS 8 更新我的应用程序,但我会确保通读文档并相应地更新它们。谢谢:)【参考方案3】:

试试这个:

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

【讨论】:

以上是关于如何限制 UIAlertView UITextField 中的字符输入的主要内容,如果未能解决你的问题,请参考以下文章

如何响应 UIImagePickerController 生成的 UIAlertView

如何更好地限制一个UITextField的输入长度

如何将变量传递给 UIAlertView 委托?

UIAlertView,如何在里面放置很多按钮?

如何设置 UIAlertView 的高度和宽度?

如何观察何时显示 UIAlertView?