Remove keyboard when table cell selected - ios

I have a modal set up like this:
Despite appearances, the first two fields are UITextFields that become first responders and display the keyboard. The third 'Department' item is a table view and pushes another view.
I have implemented a scroll view so that while either of the fields is being edited, the user can scroll around and reach the department cell:
When it is selected, before the push, I would like to hide the keyboard. It's a small detail, but try adding a new event on Apple's 'Calendar' app. It opens with the first text field as first responder (so keyboard is present). If you select the start/end cell, the keyboard hides as the next view is pushed.
How do I achieve this? As a test I tried:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"hiding keyboard");
if ([ingredientName isFirstResponder]) {
NSLog(#"resigning name field");
[ingredientName resignFirstResponder];
}
else if ([ingredientAmount isFirstResponder]){
NSLog(#"resigning amount field");
[ingredientAmount resignFirstResponder];
}
}
The logging confirms that these are getting called. But the keyboard does not hide. It simply stays in place as the view slides out. And on return, the previous field still has focus (and the keyboard is out).
Any ideas?

resignFrstResponder will not dismiss the keyboard when you're using a modal view.
Another dev found a workaround for this behavior here: http://viraj-workstuff.blogspot.com/2010/12/resignfirstresponder-does-not-hide.html

Not terribly elegant, but try stealing the firstresponder status with an ad-hoc, invisible UITextField:
-(void)dismissKeyboard {
UITextField *textField;
textField=[[UITextField alloc] initWithFrame:CGRectZero];
[self.view addSubview:textField];
[textField becomeFirstResponder];
[textField resignFirstResponder];
[textField removeFromSuperview];
// [textField release] // uncomment if not using ARC
}

Two things:
Are you using a segue for pushing that other VC? If so, resign the keyboard in the prepareForSegue method.
If not, try using willSelectRowForIndexPath instead of didSelectRowAtIndexPath

Related

Hide keyboard on button press IOS

I need to hide the IOS keyboard when i press a button on the screen.
Whatever i try, the keyboard doesn't go away when i press the button on the screen.
// This is the button
-(IBAction)showDateView:(id)sender
// hide keyboard
[self.view endEditing:YES];
}
Thanks
[self.textfield resignFirstResponder];
- resignFirstResponder
Notifies the receiver that it has been asked to relinquish its status as first responder in its window.
in general there may be more than one textfields in your screen you don't know which textField must be resigned so add all the textField objects into an array and iterate a loop to resignFirstResponder
for (uilabel *textField in labelObjArray) {
[textField/textView resignFirstResponder]
}
the key board will be resigned immediate
[self.view endEditing:YES];
This will work if the responder was initiated from the view.
[self.navigationController.navigationBar endEditing:YES]
This will work if the responder was initiated from the navigation bar.

Tapping between UITextFields in iOS7

When a UITextField is selected and has a keyboard shown, if I tap other parts of the view the keyboard disappears.
If I tap another UITextField, the keyboard stays up, the first textfield is deselected, and nothing happens. Then I need to tap on the other UITextFIeld again for selection to happen and a keyboard to appear.
Is there a way to make a second UITextField immediately accessible when a first UITextField is selected?
If you reload the tableview in textFieldDidEndEditing, you'll break selection in this way. Don't do that.
try it, press another view should call below fn.
-(void)disappearKey{
[self.view endEditing:YES];
}
after keyboard disappear, Tap any textfield, will appear keyboard.
First of all I think its a bug that the keyboard is not dismissed and opened again when tapping on another UITextField or UITextView. It should be reported and Apple should fix it.
Using the textfield delegate methods and registering for keyboard notification it should be possible to manually keep track if the user tapped on another textfield and the keyboard did not close and reopen. At the very least you should be able to detect when this is happening and close the keyboard manually by [textField resignFirstResponder];
The keyboard notification are as follows:
UIKeyboardWillShowNotification
UIKeyboardDidShowNotification
UIKeyboardWillHideNotification
UIKeyboardDidHideNotification
I'm''pretty sure you know the UITextfield and textview delegate methods
– textFieldShouldBeginEditing:
– textFieldDidBeginEditing:
– textFieldShouldEndEditing:
– textFieldDidEndEditing:
I am not in an active project at the moment so I'm not sure if I just ignored the problem but I can't recall this happening to me.
you can use BSKeyboardControls. just see the demo and decide to
use or not.
or you can do you have to set tag in sequence to the each textfield
in uiview. then use the below code.
-(BOOL)textFieldShouldReturn:(UITextField*)textField
{
NSInteger nextTag = textField.tag + 1;
UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
if (nextResponder) {
[nextResponder becomeFirstResponder];
} else {
[textField resignFirstResponder];
}
return NO;
}

dismiss Keyboard not working

Hi, I am not able to dismiss keyboard. I have a scroll view which have UITextFields on a it. I tried using
[self.view endEditing:YES];
And
[self.scrollView endEditing:YES];
I tried using resignFirstResponder on individual textfields but no use.
This issue only occurs when I tap a textfield which I am using as button when taped upon it I use
[textField resignFirstResponder];
but old one don't resign whatever I do like I tried using endEditing before I resigned the button like textfield. So my question is what could be the problem in my scenario and is there any way to forcefully dismiss keyboard?
If you want textField to act as button, use delegates.
textField.delegate=self;
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return NO;
}

Using next and previous button with UITextField, problems with firstresponder

I am using a subclass of a scrollview that moves the keyboard out of the way. (TPKeyboardAvoidingScrollView)
I think this is conflicting with my implementation of the next and previous buttons. I have built an inputaccessoryview
I have a category that sets the next and previous textfields for each field
when i edit a textfield, i set the current, previous and next textfields
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
_currentTextField = textField;
_prevTextField = [textField prevTextField];
_nextTextField = [textField nextTextField];
return YES;
}
when they click next or previous i call this method
- (void)selectAdjacentResponder:(id)sender{
UISegmentedControl *segmented = sender;
if(segmented.selectedSegmentIndex == 0){
[_prevTextField becomeFirstResponder];
} else {
[_nextTextField becomeFirstResponder];
}
}
this works fine.. but when i close the keyboard. my scrollview is messed up. if i add the line
[_currentTextField resignFirstResponder];
to the first line of my selectadjacent method it solves the problem. but the problem is it makes the screen focus in a funky way since i'm dismissing and accessing the next textfield at the same time.
i have tried resigning first responder when i close the keyboard. but i think my scrollview is calculated before that point.. any idea what to do =/

iOS - Shifting from UITextField to another doesn't start the Second One

Here is the UI:
I have a table view that has rows including text fields. For example row1 has textField1 and row2 has textField2.
Now, if the user taps the textField1, everything works fine. The delegate is called and everything.
If the user, then, taps the textField2, only the textFieldDidEndEditing: is called for textField1. textFieldDidBeginEditing: is not called for textField2 (I am pretty sure that textField2 delegate is set, because when I then tap textField2 (again), it starts editing (textFieldDidBeginEditing: is now called)
You may think this is not a real problem. The problem is that the iOS keeps showing the keyboard! With no text fields associated to it! I even loop to all text fields in all visible rows and resign them with no luck.
Seems a bug in iOS, right?
Edit: Here is the code in the delegate
- (void) textFieldDidBeginEditing:(UITextField *)textField {
[self fixTableViewOffsets];
RSMCellEditPricesCell *cell = (RSMCellEditPricesCell *) [[textField superview] superview];
[self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForCell:cell]
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
}
- (void) textFieldDidEndEditing:(UITextField *)textField {
self.currentPriceTextField = nil;
[self fixTableViewOffsets];
[self.tableView reloadData];
}
First of all, be careful with the naming conventions, if you are adding a label at the end of a variable that variable should be a UILabel not a UITextField. Second your problem might be in the textFieldDidEndEditing method, you are calling resignFirstResponder on all your text fields(if the cell.priceLabel is a text field). You shouldn't callresignFirstResponder in textFieldDidEndEditing or textFieldSouldReturn because if those methods are called the text fields are already resign as first responder.
I couldn't find a fix for it. However, as a workaround, I disabled the second text field. When the user taps this field, it will trigger the touch event on cell, not the text field which will dismiss the keyboard. Then, I enable all text fields and the user can select this second text field again.
It's not quite a solution and it doesn't provide the best UX. Still, it work.

Resources