2013-02-26 23 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]; 
}