Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
返回上一个UITextfield,IOS_Ios_Objective C_Uitextfield - Fatal编程技术网

返回上一个UITextfield,IOS

返回上一个UITextfield,IOS,ios,objective-c,uitextfield,Ios,Objective C,Uitextfield,我的应用程序有一个注册视图,我比较密码文本字段和确认密码文本字段中的字符串,如果它们不匹配,我希望用户返回密码文本字段 这个问题是通过使用标签完成的 有没有一种不使用标签的方法来实现这一点 //call next textfield on each next button - (BOOL) textFieldShouldReturn:(UITextField *) textField { BOOL didResign = [textField resignFirstResponder

我的应用程序有一个注册视图,我比较
密码文本字段
确认密码文本字段
中的字符串,如果它们不匹配,我希望用户返回
密码文本字段

这个问题是通过使用标签完成的

有没有一种不使用标签的方法来实现这一点

//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 
        }

    }
}

不使用标记,您可以创建一个描述所需顺序的文本字段输出的NSArray

像这样声明和初始化

@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];
}
像这样向后移动焦点

- (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];
}