Make "Done" button work on TextField Keyboard - objective-c

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;
}

Related

UIVIew endEditing:YES doesnt hide the keyboard

I have a UIVIew which is a subview and it contains several UITextFields. One of these textfields (which is for DATE) should not be editable using the keyboard, instead of this I use a PopOver with a Datepicker inside.
I run a method when the UIControlEventEditingDidBegin is reached. This method calls the resignFirstResponder on the DateTextField.
Everything works fine if the DateTextField is the first field to edit, but when another textField is edited and of course shows the keyboard and then try to edit the DateField, the keyboard doesn't hide and everything goes normal but with the Keyboard doing anything.
I have tried to call the method endEditing:YES before the resignFirstResponder but it doesn't work. I have tried to run the endEditing:YES and resignFirstResponder on the didEndEditing text field method but theres no way to get that keyboard out.
here is my method:
- (void)showDatePopOver:(id)sender{
[self.view endEditing:YES];
UITextField *textField = (UITextField *)sender;
[sender resignFirstResponder]; // hide keyboard
/** POP OVER LINES**/
}
You should use the textFieldShouldBeginEditing: delegate method instead of resigning first responder in didBeginEditing:
This will allow editing on ALL BUT the dateTextField text field:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return (![textField isEqual:dateTextField]);
}
You should specify that your view controller is a text view delegate as well like so (in the interface declaration [.h file]):
#interface MyViewController : UIViewController <UITextFieldDelegate>

UITableView reloadData causes UITextField to resignFirstResponder

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.

tableview with text fields in each row having resignkeypad issue

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.

Programmatically make keyboard go away

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.

Keyboard not show onload

I want the keyboad show automatically onload, but it did not work as i expect
here is the code i use:
- (void)viewDidLoad {
[super viewDidLoad];
[seachBar2 setDelegate:self];
}
- (BOOL)Searchbar2ShouldReturn:(UISearchBar *)searchBar2 {
[searchBar2 becomeFirstResponder];
return YES;
}
Coud somebody will point me how to fix this
thank you somuch
- (void)viewDidLoad {
[super viewDidLoad];
[seachBar2 setDelegate:self];
[seachBar2 becomeFirstResponder];
}
Just came across this, and it helped but the answers are vague.
All you need to do now in iOS 5 is make an outlet connection to your object (for example a UITextField) and then in viewDidLoad method type;
[myTextField becomeFirstResponder];
or for your search bar
[searchBar2 becomeFirstResponder];
Make sure searchBar2 in your code points (IBOutlet) to the searchBar in the Interaface Builder.
in your code:
IBOutlet UISearchBar *searchBar2;
In IB:
goto the search bar's Connections Inspector (apple-2) and drag the Referencing Outlet to File Owner and select searchBar2
Hope this helps.
You do need to override viewDidAppear:, and verify it's actually being called (put a breakpoint or an NSLog() statement in there). You should also determine the language you're coding in (it's Objective C).
Your -Searchbar2ShouldReturn: method will never be called by the system. I think you may need to go back and work through a few of Apple's tutorials here; your grasp of the frameworks seems tenuous, at best.