how to resignFirstResponder from view controller - objective-c

How to resignFirstResponder from uiviewcontroller object with is active.
I have five textfields one,two,three,four,five.
- (void)removeKeyBoard{
[one resignFirstResponder];
[two resignFirstResponder];
[three resignFirstResponder];
[four resignFirstResponder];
[five resignFirstResponder];
}
The above code has done my job, but each object reference to specify for resignFirstResponder.
Is there any way, that handle the resignFirstResponser which ever field is active on UIViewController automatically resign that object.
#All
Thanks in advance.

Called on whatever view has the textfields in it:
[view endEditing:YES]
UIView reference: Link

- (void)textFieldDidEndEditing:(UITextField *)textField {
[textField resignFirstResponder];
}

Related

Programmatic UITextField and programmatically releasing the keyboard

I've looked in many places and it seems everyone uses IB. I like it, but find it more fun writing it all out. This being said, I'm having difficulty dropping my keyboard after editing is done. Here's an example.
-(void)textStuff
{
UITextField *someField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];//not true size
}
Here I've tried the resign firstresponder and even a BOOL to say are you editing, YES, then make that keyboard first responder dangit....oh, you're done editing...good, now resign that first responder.....none has worked as of yet. Any help would be greatly appreciated.
Thanks
1).
[self.view endEditing:YES];
2).you set the delegate method.
SOMEFIELD.text =delegate;
And .h file
#interface yourViewController : UIViewController<UITextFieldDelegate>
and in .m file you add the delegate method of the textfield
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
all you have to do is set someField's delegate your view controller and implement following method,
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}

Cannot get DONE button on UITextField to work

I cannot seem to get the DONE button on the keyboard to dismiss the keyboard? Anyone know how to make it work?
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
[textView resignFirstResponder];
return YES;
}
Your code, in particular resigning first responder of the textfield in -textFieldShouldReturn is fine.
I think you probably didn't hook things up correctly, possibly forgetting to set the delegate of your UITextField.
A snippet from a recent project of mine where I used a Next return key and a Done return key for two fields, toggling from the first to the second, and dismissing the second (which had a Done return key):
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
if(textField == self.nameTextField)
[self.descriptionTextField becomeFirstResponder];
return YES;
}
Concerning UITextView (your question specifically mentions UITextField in the title but your code has both for textfield and textview), you will need to resign first responder differently.
Since Text views are for longer text entry including multiple lines where you can also use the return key for a line break, you'll have to detect when the user changes the text.
If they try to enter a line break, then manually resign at that point.
Example follows:
- (BOOL)textView:(UITextView *)textView
shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
if([text isEqualToString:#"\n"])
[textView resignFirstResponder];
return YES;
}

resignFirstResponder - can not remove keyboard

I have a multi view app, to swap views I use:
-(IBAction) goSecond{
[self presentModalViewController:secondviewController animated:YES]
}
On this view I have a text field that the user will enter a number, but I con not get the keyboard to remove from the view. I have read several posts but can get this to work. I use:
-(IBAction) goAwayKey: (id) sender{
[sender resignFirstResponder];
}
-(IBAction) tapBack: (id) sender{
[textField1 resignFirstResponder];
}
In secondviewController.h I have
-(BOOL) disableAutomaticKeyboardDismissal;
and in secondviewController.m
-(BOOL) disableAutomaticKeyboardDismissal{
return NO;
}
I still cant get the keyboard to go away, is there anything obvious that I am missing?
Thanks for your help.
Theoretically,
[textField1 resignFirstResponder];
should work fair enough, are you sure that your methods are being called? Place a breakpoint in both methods to resign and check if they are called, that will give us some clues about your problem.

Keyboard resignfirstresponder when Click Done

I have a textfield called searchBox and am trying to get rid of the keyboard if the user either clicks on Done.
Is there an IBAction that I need to know to do this?
You have to make your viewcontroller an UITextFieldDelegate and write in
viewDidLoad:
[searchBox setDelegate:self];
And then write:
- (BOOL)textFieldShouldReturn:(UITextField *)textfield
{
[searchbox resignFirstResponter];
return YES;
}
In your view controller:
- (void)viewDidLoad
{
// ...
searchBox.delegate = self;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[searchBox resignFirstResponder];
return YES;
}
See the UITextFieldDelegate documentation for details (you might want to return NO instead of YES in my example).

How to hide the keyboard in objective-c

I have a simple view with a textbox and a UIButton. When I click the UIButton I simply want to hide the keyboard that is currently in the view. Is this a simple delegate I can add to the controller or something more complex?
Of the answers that exist on SO already I haven't found one that has a full solution for this context. Any help would be great!
Try something like:
[TextField resignFirstResponder];
Where TextField is the name of your text field.
This is how you hide the UITextField when you hit the return button:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// do whatever you have to do
[textField resignFirstResponder];
return YES;
}
This is how you hide when you hit an UIButton:
- (void)hideTextField:(UITextField *)textField {
// do whatever you have to do
[textField resignFirstResponder];
}