2013-02-26 26 views
0

我的应用程序已经登记的看法,我比较字符串在password textfieldconfirm password textfield,如果他们不匹配我希望用户回到password textfield回到以前的UITextField,IOS

这个问题与使用标签做了 UITextField jump to previous

有没有办法在不使用标签的情况下完成此操作?

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

    } 
} 

回答

0

如果不使用标签,您可以创建一个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]; 
}