I have a textField that is set to change the tableView's dataSource with each letter that's entered (and call reloadData).
But for some reason, every time a letter is entered, the keyboard is dismissed.
Anyone know why?
Your text field is resigning because reloaded cells are sent a -resignFirstResponder message due to the fact that their survival is not guaranteed after a reload. See this related question for more.
Use this method textFieldShouldReturn: and add UITextFieldDelegate delegate in yourClass.h file. set delegate to yourTextfield and write following code in viewDidLoad method.
yourTextfield.delegate = self;
and also implement the textFieldShouldReturn: as following as
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
[theTextField resignFirstResponder];
return YES;
}
I think it will be helpful to you.
Related
I have an NSTextFeild Subclass in which I would like to implement textDidEndEditing: to check after each edit if it is empty or not. The method is being called perfectly, but when I click into another NSTextField (or a subclass), all the text that was in the first textfield is immediately deleted. If I click out into the view, the text stays, but is deleted the next time i click into another textfeild. All I have in the method right now is an NSLog. Does anyone have any ideas as to why this could be happening?
#import "BufferTableCellViewTextField.h"
#implementation BufferTableCellViewTextField
- (void)textDidEndEditing:(NSNotification *)notification{
NSLog(#"END");
}
#end
You need to call -super:
- (void)textDidEndEditing:(NSNotification *)notification;
{
[super textDidEndEditing:notification];
NSLog(#"END");
}
This delegate method wouldn't be causing your problem. I'd look back to your subclass and check to see that you are not using any UITextField delegate methods that are expecting a YES response that you may have inadvertently changed the return responses for. It may be helpful to post you subclass so we can see what is going on in there too...
I have a toolbar in my UITextfields' inputAccessoryView. If the 'next' button is hit it makes the next textfield in an orderedSet of all my textfields become the first responder. THAT works perfectly.
BUT I can't get the 'previous' textfield to becomeFirstResponder.
I've checked in the console and the textfield does call textFieldShouldBeginEditing and I am returning YES but it never calls textFieldDidBeginEditing and the textfield that should resignFirstResponder never calls textFieldShouldEndEditing.
So the textfield is getting the message to becomeFirstResponder but doesn't.
- (IBAction)keyboardBarButtonDidTouch:(UIBarButtonItem *)sender
{
if (sender==self.previousBarButton&&self.activeTextFieldArrayIndex.intValue>0)
{
[(UITextField *)[self.textfields objectAtIndex:(self.activeTextFieldArrayIndex.intValue-1)] becomeFirstResponder];
}
if (sender==self.nextBarButton&&self.activeTextFieldArrayIndex.intValue<(self.textfields.count-1))
{
[(UITextField *)[self.textfields objectAtIndex:(self.activeTextFieldArrayIndex.intValue+1)] becomeFirstResponder];
}
if (sender==self.doneBarButton)
{
[self.activeTextField resignFirstResponder];
}
}
The weirdest thing is that if I force the textfield to resignFirstResponder by some action that just does that, the 'previous' textfield suddenly becomesFirstResponder. Like it was being added to a list of responders and it became its turn....
And as is often the case when strange things happen, it was unrelated to becoming first responder. I was accidentally changing my activeTextfield pointer to nil in the middle of a bunch of logic by way of the textfield delegate. ^o^
Disregard!
1) Try making your code less error prone.
instead of this
if(sender==self.previousBarButton&&self.activeTextFieldArrayIndex.intValue>0)
do this
if ((sender==self.previousBarButton) && (self.activeTextFieldArrayIndex.intValue>0))
2) use textfield.tag property and set unique tags for these text fields. and do your stuff about become/resign first reponders by using
(uitextfield *)field[self viewWithTag: uniqueTag];
When adding a textfield, the keyboard opens correctly, however I cannot get the done button to work properly. I know thee are other similar posts, however for whatever reason they do not seem to work for me.
When I say "not work" i mean the keyboard does not close.
Any suggestions would be appreciated.
Add this and let me know:
- (BOOL)textFieldShouldReturn:(UITextField*)aTextField
{
[aTextField resignFirstResponder];
return YES;
}
Remember to add the viewController as delegate of your text field
I believe this is what you're looking for. Its a UITextFieldDelegate callback thats called anytime the Done/Return button is used on the keyboard.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
You'll need the delegate in your interface
#interface Class : UIViewController <UITextFieldDelegate>
If you're textfield is in a ModalViewController using the FormSheet style you need this as well.
- (BOOL)disablesAutomaticKeyboardDismissal
{
return NO;
}
i am adding the textfield on each row inside the cellforrow method of tableview,but when i type anything on any textfield ind try to resign textfield on view touch, but it only works for last row's textfield only. what is the solution for that issue,
please help.
Thanks
Dhananjay
if i understand you correctly this will probably work:
add the protocol to your viewController in question and add
- (void)textFieldDidEndEditing:(UITextField *)textField {
[textField resignFirstResponder];
// custom code
}
afterwards make sure that each textField has your viewController as delegate.
I made a text field but I didn't use Interface Builder, I did it programmatically in Xcode. So now I need a programmatic way to make it resign first responder so that the keyboard will go away when the user presses enter.
[textField resignFirstResponder];
docs
if you want it to go away when enter is pressed, you will need to implement
- (BOOL)textFieldShouldReturn:(UITextField *)textField
in your UITextFieldDelegate
As addition to cobbal's answer, don't forget to set text field's delegate to the class that implements
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
Adding descriptor to that class interface declaration is also a good thing.
I had this question my self and I realize that the above answer may not be what you're asking. My guess is you are adding UITextField as subviews to some view when you 'say double tap' (or some other event). It is important to make a controller the delegate of that subview.
-(void)handleDoubleTapGesture:(UITapGestureRecognizer *)gestureRecognizer{
//some other code creating the textfield
[aTextField setDelegate:self];
In this case I've used the controller itself.
Now inside that controller I can implement
-(BOOL) textFieldShouldReturn:(UITxtField *)textField
and it will work.