Saman
Saman

Reputation: 523

How to BecomeFirstResponder the UiTextField after each UiButton's Enter action in Objective-C

I have UITextField & UIButton. I want to becomeFirstResponder my TextField after each Enter action (my Button action) but it doesn't work ! here is my code :

[_textfield becomeFirstResponder];

[_textfield addTarget:self action:@selector(backAction:) 
forControlEvents:UIControlEventEditingDidEndOnExit];

[_back addTarget:self action:@selector(backAction:) 
forControlEvents:UIControlEventTouchUpInside];

- (void)backAction:(id)sender
{
    [users addObject:_textfield.text];
    _textfield.text = nil;
    [_textfield becomeFirstResponder];
}

Upvotes: 0

Views: 291

Answers (2)

QUserS
QUserS

Reputation: 512

Instead of "backaction:" add a separate action for textfield.

[_textfield addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventEditingDidEndOnExit];

And in this targetted method,

- (void)dismiss {
[self performSelector:@selector(backAction:) withObject:nil afterDelay:0]}

It seems to be working when I tried.

On clicking return, editing gets completed and keyboard resigns. But 'backaction:' will get triggered before that and the keyboard will disappear. Hope this helps.

Upvotes: 1

Vinay Kumar
Vinay Kumar

Reputation: 107

Please check you have give textfield delegate to view controller or add code

_textfiled.delegate = self

Upvotes: 0

Related Questions