1

问题:

我正在尝试创建一个自定义 UITextField 类“UIValidatedTextField”,它允许设置某些规则来确定输入是否有效。例如,您可以设置一个正则表达式参数以确保输入是特定格式的,即密码、电子邮件地址等...

另一个功能是指定和设置引用另一个 UITextField 的参数,并确保输入与来自另一个 UITextField 的输入匹配。

我在这里遇到的问题是我将此引用设置为另一个 UITextField。但是,当我访问它的“文本”字段时,我发现即使我在其中输入了一些内容,文本字段中也没有任何内容。

我在下面提供了相关代码:

#import "UIRegisterViewController.h"
#import "UIRegisterViewCell.h"
#import "UIValidatedTextField.h"
#import "NSConstants.h"

@interface UIRegisterViewController ()


@end

@implementation UIRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableItems = @[@"name", @"email", @"netId", @"username", @"password", @"confirmPassword"];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_tableItems count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellIdentifier = [_tableItems objectAtIndex:indexPath.row];
    UIRegisterViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];


    if ([cellIdentifier isEqualToString:@"email"]) {
        [cell.textField setRegex:VALID_DUKE_EMAIL_REGEX];

    } else if ([cellIdentifier isEqualToString:@"netId"]) {
        //Validation?

    } else if ([cellIdentifier isEqualToString:@"username"]) {
        //Validation?

    //THIS IS THE CELL THAT I WANT TO COMPARE INPUT TO
    } else if ([cellIdentifier isEqualToString:@"password"]) {
        [cell.textField setRegex:VALID_PASSWORD_REGEX];

    //SETTING THE TEXT FIELD IN QUESTION HERE...
    } else if ([cellIdentifier isEqualToString:@"confirmPassword"]) {
        [cell.textField setRegex:VALID_PASSWORD_REGEX];
        NSIndexPath *index = [NSIndexPath indexPathForRow:4 inSection:0];
        UIRegisterViewCell *confirm =(UIRegisterViewCell *)[self tableView:_tableView cellForRowAtIndexPath:index];
        [cell.textField setConfirm:confirm.textField];
    }

    cell.textField.delegate = self;
    return cell;
}

#pragma mark - Text Field Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}


@end

请注意,textFields 是 UIValidatedTextFields - 下面提供的自定义类:

    #import "UIValidatedTextField.h"
#import "NSArgumentValidator.h"

@implementation UIValidatedTextField

- (id) initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initialize];
    }
    return self;
}

- (id)initialize {
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:)
                name:UITextFieldTextDidChangeNotification object:self];
        [self validate]; //Validate in case editing began before observer was set.
    }
    return self;
}

- (void) setOptional:(BOOL)isOptional {
    _isOptional = isOptional;
}

- (BOOL) isOptional {
    return _isOptional;
}

- (void) setRegex:(NSString *)regex {
    _regex = regex;
}

//SET THE TEXT FIELD TO COMPARE INPUT AGAINST HERE.
- (void) setConfirm:(UITextField *)confirm {
    _confirm = confirm;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:)
            name:UITextFieldTextDidChangeNotification object:_confirm];
    [self validate]; //Validate in case editing on confirm began before observer was set.
}

- (void) setQuery:(NSString *)query {
    _query = query;
}

- (void) textFieldDidChange:(NSNotification *)notification {
    NSLog(@"UPDATE");
    _isValid = [self validate];
    [self showInputValidation];
}

- (BOOL) validateRegex {
    if (_regex.length == 0) {
        return true;
    }
    return [NSArgumentValidator isValid:self.text withRegex:_regex];
}

- (BOOL) validateConfirm {
   // NSLog(@"%@ : %@", [_confirm text], self.text);
    if (_confirm == NULL) {
        //NSLog(@"IS NULL");
        return true;
    }
    return [self.text isEqualToString:_confirm.text];
}

- (BOOL) validateQuery {
    return true;
}

- (BOOL) validate {
    _isValid = (self.text == 0 && _isOptional) || ((self.text != 0) && [self validateRegex] && [self validateConfirm] && [self validateQuery]);
    return _isValid;
}

//IF ANYONE HAS A SOLUTION AS TO HOW TO MAKE CHANGING BORDER COLOR CHANGE THE COLOR ALONG THE ROUNDED BORDER THAT IS PRESENT AS OPPOSED TO A RECTANGULAR BORDER LET ME KNOW.
- (void) showInputValidation {
    self.layer.borderWidth = 1.0;
    if (self.text.length == 0) {
        self.layer.borderColor = [[UIColor blackColor] CGColor];
    } else if (_isValid) {
        self.layer.borderColor = [[UIColor greenColor] CGColor];
    } else {
        self.layer.borderColor = [[UIColor redColor] CGColor];
    }
}
- (void) finalize {
    [super finalize];
    [[NSNotificationCenter defaultCenter] removeObserver:self
            name:UITextFieldTextDidChangeNotification object:self];

    if (_confirm != NULL) {
        [[NSNotificationCenter defaultCenter] removeObserver:self
                name:UITextFieldTextDidChangeNotification object:_confirm];
    }
}

@end

谢谢您的帮助!

4

1 回答 1

0

此代码中的一个明显错误是您将正则表达式设置为cellForRowAtIndexPath:,即使所有单元格都在重用相同的单元格对象。cellForRowAtIndexPath:应该只用于设置单元格内容,如文本和颜色。相反,创建IBOutlet验证文本字段并将其正则表达式添加到viewDidLoad. 更好的是,完全废弃自定义子类,而是在编辑完成时,只要相关文本字段之一触发事件,就运行正则表达式验证。

于 2014-09-05T00:38:49.173 回答