xcode 在 ObjectiveC 中设置对 UITextField 的限制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37719335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set limit to UITextField in ObjectiveC
提问by Mohith P
I am creating login screen using Objective C in which i want to implement validation for User name(E-Mail) and password.How to implement this in very simple way.
我正在使用 Objective C 创建登录屏幕,我想在其中实现用户名(电子邮件)和密码的验证。如何以非常简单的方式实现这一点。
回答by Sandeep Bhandari
You can always make use textField shouldChangeCharactersInRange delegate to handle the number of characters allowed in textField. Have look at the solution provided below :) Hope it helps
您始终可以使用 textField shouldChangeCharactersInRange 委托来处理 textField 中允许的字符数。看看下面提供的解决方案:)希望它有帮助
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if(textField == self.emailTextField){
if (textField.text.length < 30 || string.length == 0){
return YES;
}
else{
return NO;
}
}
}
EDIT
编辑
As per your comments you are not recieveing the textField delegates, so here is what you can do :)
根据您的评论,您没有收到 textField 委托,因此您可以这样做:)
In your ViewController, confirm the UITextFieldDelegate using,
在您的 ViewController 中,确认 UITextFieldDelegate 使用,
YourViewController : UIViewController <UITextFieldDelegate>
In your viewDidLoad or ViewWillAppear set the textField delegate as self.
在您的 viewDidLoad 或 ViewWillAppear 中,将 textField 委托设置为 self。
self.emailTextField.delegate = self;
回答by Iyyappan Ravi
If you click the Validate Button, set the validate button Action and see the below code,first check the two textfield empty or not, if you give text then again check its valid email type or not, then all conditions are true, then put your code and run,
如果您单击验证按钮,设置验证按钮操作并查看以下代码,首先检查两个文本字段是否为空,如果您提供文本,则再次检查其有效的电子邮件类型与否,则所有条件都为真,然后将您的编码并运行,
-(IBAction)action:(id)sender
{
if (tfMail.text.length == 0 || tfPass.text.length == 0)
{
[self validatetextfield];
}
else if (![tfMail.text isEqualToString:@""])
{
NSString *emailRegEx = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx];
//Valid email address
if ([emailTest evaluateWithObject:tfMail.text] == YES)
{
//Its validated put your success code,
}
else
{
UIAlertController *aler = [UIAlertController alertControllerWithTitle:@"Test!" message:@"Please Enter Valid Email Address. \nex. [email protected]" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self dismissViewControllerAnimated:YES completion:nil];
}];
[aler addAction:action];
[self presentViewController:aler animated:YES completion:nil];
//not valid email address
}
}
}
Alert Message Method:
告警消息方式:
-(void) validatetextfield
{
if (tfMail.text.length==0) {
UIAlertController *aler = [UIAlertController alertControllerWithTitle:@"Email Field Empty!" message:@"Please Enter the Email" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self dismissViewControllerAnimated:YES completion:nil];
}];
[aler addAction:action];
[self presentViewController:aler animated:YES completion:nil];
[tfMail becomeFirstResponder];
}
else if (tfPass.text.length==0)
{
UIAlertController *aler = [UIAlertController alertControllerWithTitle:@"Password Field Empty!" message:@"Please Enter the Password" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self dismissViewControllerAnimated:YES completion:nil];
}];
[aler addAction:action];
[self presentViewController:aler animated:YES completion:nil];
[tfPass becomeFirstResponder];
}
}
its successfully working for me, hope its helpful.
它成功地对我来说有效,希望它有帮助。