0

I am trying to detect if certain textfield has been clicked in an End On Exit event for the current selected TextField. Let me show you instead of trying to explain:

- (IBAction)txtUsernameEdit:(id)sender {
    CGRect loginButton = btnLogin.frame;
    CGRect registerButton = btnRegister.frame;
    loginButton.origin.x = 19;
    loginButton.origin.y = 175;
    registerButton.origin.x = 189;
    registerButton.origin.y = 175;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];  
    btnLogin.frame = loginButton;
    btnRegister.frame = registerButton;    
}

- (IBAction)txtUsernameEndExit:(id)sender {
    if (!txtPassword.isTouchInside) {
    CGRect loginButton = btnLogin.frame;
    CGRect registerButton = btnRegister.frame;
    loginButton.origin.x = 19;
    loginButton.origin.y = 308;
    registerButton.origin.x = 189;
    registerButton.origin.y = 308;

    [UIView beginAnimations:nil context:nil];    
    [UIView setAnimationDuration:0.5];
    btnLogin.frame = loginButton;
    btnRegister.frame = registerButton;
    }
}

- (IBAction)txtPasswordEdit:(id)sender {
    CGRect loginButton = btnLogin.frame;
    CGRect registerButton = btnRegister.frame;
    loginButton.origin.x = 19;
    loginButton.origin.y = 175;
    registerButton.origin.x = 189;
    registerButton.origin.y = 175;

    [UIView beginAnimations:nil context    :nil];
    [UIView setAnimationDuration:0.5];    
    btnLogin.frame = loginButton;
    btnRegister.frame = registerButton;
}

- (IBAction)txtPasswordEndExit:(id)sender {
    if (!txtPassword.isHighlighted) {
    CGRect loginButton = btnLogin.frame;
    CGRect registerButton = btnRegister.frame;
    loginButton.origin.x = 19;
    loginButton.origin.y = 308;
    registerButton.origin.x = 189;
    registerButton.origin.y = 308;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    btnLogin.frame = loginButton;
    btnRegister.frame = registerButton;
    }
}

So I have two textfields txtUsername and txtPassword. the functions txtUsernameEndExit and txtPasswordEndExit are fired when the user clicked on the opposite textfield or the done key on the keyboard. How can I find out in those functions if the user dit actually click there?

In the if-clause I tried the following attributes and methods: - isFirstResponder - isTouchInside - isEditing - isHighlighted

But I guess that none of these attributes have been changed if the End On Exit function hasn't been fired yet.

What would be the appropriate approach?

EDIT - DIFFERENT APPROACH So, recently I figured that I could also observer the keyboard's show or hide. I have autolayout enabled for my view and there are only two textfields and two buttons on it. As soon as I tap a textfield, the buttons come up as the animation describes and the go down again (apparently autoreverse does this) but if I tap a textfield and afterwards tap another textfield the keyboard doesn't go down but the buttons do. Here's all the code I used to animate this:

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardDidShow:)
                                                 name:UIKeyboardDidShowNotification
                                               object:nil];
}

- (void)keyboardDidShow: (NSNotification *) notif{
    CGRect loginButton = btnLogin.frame;
    CGRect registerButton = btnRegister.frame;
    loginButton.origin.x = 19;
    loginButton.origin.y = 175;
    registerButton.origin.x = 189;
    registerButton.origin.y = 175;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    btnLogin.frame = loginButton;
    btnRegister.frame = registerButton;
}
4

1 回答 1

1

为什么不使用 UITextFieldDelegate 方法:

  • textFieldShouldBeginEditing:
  • textFieldDidBegin编辑:
  • textFieldShouldEnd 编辑:
  • textFieldDidEnd编辑:
于 2013-11-10T21:03:03.960 回答