ios - Objective C 中的表单验证

标签 ios objective-c iphone ipad objective-c-blocks

我正在创建一个注册页面。该页面具有以下文本字段 FirstName , LastName , Gender , Email , Phone , PasswordConfirm_Password .在注册之前,我需要验证这些字段。首先,我将检查是否有任何字段为空。如果任何字段为空,则应显示一条消息。我有以下代码。我知道代码很简单而且太长了。我的问题是我们如何通过减少代码来编写相同的功能?我们如何以标准方式编写相同的代码?

 if([FirstName.text isEqualToString:@""])
{
    NSLog(@"FirstName should not be empty");
}
else if ([LasttName.text isEqualToString:@""])
{
   NSLog(@"LastName should not be empty");
}
else if ([Gender.text isEqualToString:@""])
{
    NSLog(@"Gender should not be empty");
}
else if ([Email.text isEqualToString:@""])
{
     NSLog(@"Email should not be empty");
}
else if ([Phone.text isEqualToString:@""])
{
     NSLog(@"Phone should not be empty");
}
else if ([Password.text isEqualToString:@""])
{
     NSLog(@"Password should not be empty");
}
else if ([Confirm_Password.text isEqualToString:@""])
{
     NSLog(@"Confirm_Password should not be empty");
}
else{
    NSLog(@"Signup Successful");
}

最佳答案

这是@norders 答案的示例代码:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

// Delcare IBOutletCollection in the header file
@property (nonatomic, strong) IBOutletCollection(UITextField) NSArray *fields;

@end

将您的文本字段连接到您的 socket :

enter image description here

现在在您的 ViewController.m 中执行以下操作:
- (void) viewDidLoad {
    [super viewDidLoad];
    // Define placeholders - make sure you insert the placeholders in the same order as you place you textfields into the collection
    NSArray<NSString *>* placeholders = @[@"First name", @"Last name"]; 
    // Setting up the placeholders
    for (int i = 0; i < placeholders.count; i++) {
        UITextField* field = self.fields[i];
        NSString* placeholder = placeholders[i];
        field.placeholder = placeholder;
    }
}

- (IBAction)didPressValidate:(UIButton*)sender {
    [self validate:self.fields];
}

// Function to be called upon validation
- (void)validate:(NSArray<UITextField *>*)labels {
    NSMutableArray<UITextField *>* invalidFields = [NSMutableArray new];
    for (UITextField* field in self.fields) {
        if ([field.text isEqualToString:@""]) {
            [invalidFields addObject:field];
        }
    }
    [self displayWarningForInvalidLabesIfNeeded: invalidFields];
}

// Function to be called to show the error message
- (void)displayWarningForInvalidLabesIfNeeded:(NSArray<UITextField *>*)invalidFields {
    if ([invalidFields count] == 0) {
        // All labels are valid, no need for error message
        return;
    }

    NSMutableString* invalidFieldsMessage = [NSMutableString new];
    // Create string from invalid fields placeholder
    for (UITextField* field in invalidFields) {
        [invalidFieldsMessage appendString: field.placeholder];
        [invalidFieldsMessage appendString: @"\n"];
    }

    // Show the alert with the invalid fields
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Invalid fields" message:invalidFieldsMessage preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // code for handling when user pressed OK
        NSLog(@"ok pressed");
    }];

    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
}

最终结果应该是这样的:
enter image description here

关于ios - Objective C 中的表单验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40486047/

相关文章:

ios - 当@objc 和@nonobjc 在swift 中写在方法和变量之前?

iOS:StatusBar 外观错误

iphone - 如何将 HTML 代码更改为符号 iphone

c# - 如何从您的应用程序添加联系人并将其保存在地址簿中?

objective-c - NSTextFieldCell 委托(delegate)?

iphone - 应用程序像 iphone 4 一样在 iphone 5 上运行

ios - UITabBarIcon 不可见直到下一次勾选

ios - 使用 getcov 和 lcov 的 xcode7 代码覆盖率

ios - 在 ios 上使用 visual studio 2015 进行远程调试时出错(惰性符号绑定(bind)失败 : Symbol not found: _map_fd)

ios - 为什么我不能将我的 UITextView 连接到我的 .m 文件?