0

我的应用程序有一个注册视图,我比较字符串password textfieldconfirm password textfield,如果它们不匹配我希望用户返回password textfield

这个问题是通过使用标签 UITextField 跳转到上一个来完成的

有没有办法在不使用标签的情况下做到这一点?

//call next textfield on each next button
- (BOOL) textFieldShouldReturn:(UITextField *) textField {

    BOOL didResign = [textField resignFirstResponder];
    if (!didResign) return NO;

    if ([textField isKindOfClass:[SOTextField class]])
        dispatch_async(dispatch_get_current_queue(),
                       ^ { [[(SOTextField *)textField nextField] becomeFirstResponder]; });

    return YES;

}

-(void)textFieldDidEndEditing:(UITextField *)textField{

    if (textField==self.confirmPassword) {

        if ([self.password.text isEqualToString:self.confirmPassword.text]) {
            NSLog(@"password fields are equal");

        }
        else{
            NSLog(@"password fields are not equal prompt user to enter correct values");
            //[self.password becomeFirstResponder]; doesnt work 
        }

    }
}
4

1 回答 1

0

在不使用标签的情况下,您可以创建一个 NSArray 的 textfield outlets 来描述所需的顺序。

像这样声明和初始化......

@property(nonatomic,strong) NSArray *textFields;

- (NSArray *)textFields {
    if (!_textFields) {
        // just made these outlets up, put your real outlets in here...
        _textFields = [NSArray arrayWithObjects:self.username, self.password, nil];
    }
    return _textFields;
}

您需要获取当前具有焦点的文本字段,如果有...

- (UITextField *)firstResponderTextField {

    for (UITextField *textField in self.textFields) {
        if ([textField isFirstResponder]) return textField;
    }
    return nil;
}

然后像这样推进焦点......

- (void)nextFocus {

    UITextField *focusField = [self firstResponderTextField];

    // what should we do if none of the fields have focus?  nothing
    if (!focusField) return;
    NSInteger index = [self.textFields indexOfObject:textField];

    // advance the index in a ring
    index = (index == self.textFields.count-1)? 0 : index+1;
    UITextField *newFocusField = [self.textFields objectAtIndex:index];
    [newFocusField becomeFirstResponder];
}

然后像这样向后移动焦点......

- (void)previousFocus {

    UITextField *focusField = [self firstResponderTextField];

    // what should we do if none of the fields have focus?  nothing
    if (!focusField) return;
    NSInteger index = [self.textFields indexOfObject:textField];

    // backup the index in a ring
    index = (index == 0)? self.textFields.count-1 : index-1;
    UITextField *newFocusField = [self.textFields objectAtIndex:index];
    [newFocusField becomeFirstResponder];
}
于 2013-02-26T21:22:48.950 回答