gpt4 book ai didi

ios - Objective C 中的表单验证

转载 作者:行者123 更新时间:2023-12-01 17:48:14 25 4
gpt4 key购买 nike

我正在创建一个注册页面。该页面具有以下文本字段 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com