Tapping on a UIButton and hide KeyBoard in iOS7 - objective-c

I have a login screen,when all UITextField are filled user will tap on UIButton for Sign me in. If user tap on background the keyboard will be hide.
But i want to hide keyBoard when user clicked on UIButton which is sign me in button.When UIButton action TouchUpInside called,at that time i want to hide the keyboard. I do not want to hide keyboard on the tap of anywhere in the view.
Thanks in advance.

Call [yourTextField resignFirstResponder]; in your loginAction
- (IBAction)loginAction:(id)sender {
[yourTextField resignFirstResponder];
// your rest code goes here...
}

Just add this to the method in which you use the button for example:
- (IBAction)yourButtonMethod:(id)sender {
// Your operations
[self.yourTextFieldName resignFirstResponder]
}

Note: You must set delegate to self
Eg:
yourTextField.delegate=self;
Then:
- (IBAction)HideKeyBoard:(id)sender
{
[yourTextField resignFirstResponder];
}

just call resignFirstResponder on you textfield. like
[myTextField resignFirstResponder]; in IBACtion of button.

Related

Call textFieldDidEndEditing of UITextField when press UIButton

Is there anyway to call textFieldDidEndEditing of UITextField when pressed UIButton?
Current process is function in textFieldDidEndEditing can work when user press "Return" or "Done" button and click on UIButton. To make it clear, if ABC function in textFieldDidEndEditing to be run, user must press "Return" or "Done" button of Keyboard and then click on UIButton.
What I want is I want to run function in textFieldDidEndEditing can work when user press UIButton.
In the button handler method, call:
[self.view endEditing:YES];
This will force the keyboard to disappear and whatever the current text field had the focus will resign first responder and the textFieldDidEndEditing: method will be called for it.
The above assumes the button handler is in the view controller class.
Since you are dealing with a custom cell and the button is in the custom cell, simply do:
[self endEditing:YES];
where self is the custom cell. This will resign any text field in the cell.
You can use textfield UIKeyboardWillHideNotification Notification
- (void)viewDidLoad
{
[super viewDidLoad];
// register for keyboard dismiss notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillHide:(NSNotification *)notification
{
// This function will be called whenever the keyboard hides. So you can implement your textFieldDidEndEditing code here.
}
Also when your button is clicked just resign the textfield by
[yourTextField resignFirstResponder];

Display modally custom keyboard view controller for editing a UITextField

By default when tapping on a UITextField iOS will display a default keyboard. Is it possible to bypass this? I would like to display modally a custom view controller on tap on the textField and be able to edit the textField through this controller.
Is there a recommended way?
Following wil repalce the keyboard as the input view when the user clicks on the UItextField.
self.TextField.inputView = "your view ";
Ok tried out the exact requirement you asked for:-
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[textField resignFirstResponder];
POCModalViewController *objPOCModalViewController = [[POCModalViewController alloc]init];
[self presentViewController:objPOCModalViewController animated:YES completion:nil];
return NO;
}
Where POCModalViewController is the controller you want to present.
I would like to post the solution i have finally implemented, which is the closest to Footyapps27 solution:
I have made the controller that will present the modal controller(which will contain internally multiple custom keyboard views) as the uitextfield delegate for any UITextField objects contained within the view of my controller.
I can now received any notification through the - (BOOL)textFieldShouldBeginEditing:(TWValueInput *)textField method when a textfield start to be edited:
Within that delegate method I have the following code snippet:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
MyCustomKeyboardVC* vc = [[UIStoryboard storyboardWithName:#"main" bundle:nil] instantiateViewControllerWithIdentifier:#"customKeyboardController"];
vc.delegate = self;
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:vc animated:NO completion:nil];
return NO;
}
returning NO within that method will prevent the default keyboard from being displayed. There is no need actually to call the resignFirstResponderon the textfield.
I should point out though that the Apple recommended way to display a custom keyboard is to provide a custom view to the textfield inputView property like Divya mentioned. Since i wanted to managed multiple keyboard view entries it was quicker for me to display a custom keyboard controller through the delegate method i mentioned above.

iOS7:editable textfield cells in tableview is not working properly

Editable textfield cells Tableview is causing problem on keyboard tab button everytime it is calling textfieldshouldbeginediting even if i am in first textfield it is not going to nextfield.
It is going to last textfield and if popover is availabe it will crash.How can i fix this so that if enter tab then it has to resign current responder in textfielddidendediting and it should not go to textfieldshouldbegin editing.
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
PickerViewController *selectOperatorController;
NSLog(#"tag %d",textField.tag);
return NO;
}
I also declared textfield delegates like didendediting and shouldendediting
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
activeField = nil;
if (self.chooseOperatorController) {
[self.chooseOperatorController dismissPopoverAnimated:YES];
}
return YES;
}
This is not as issue in iOS 6.But it is in iOS 7.
textfieldshouldbegin editing will not allow desktop keyboard tab button input,it cannot judge to move between the textfields.If we add textfielddidbeginediting we can move the controls,eventhough we have two methods we can move by using keyboard tab.So textfielddidbeginediting is mandatory if we want to move bewtween available textfields.

UITextField in aUIScrollView - inputView doesn't work

I have a UItextField in a UIScrollView. I assigned an inputView (a picker) on my UITextField but it doesn't work. When I click on my textfield, I get the keyboard instead of a picker. When I delete the UIScrollView it works well (I get the picker). Do you have any ideas? Thanks.
Edit (more clear): I have a UItextField in a UIScrollView. I call a method that shows a picker. But the method is never called when my textfield is in a scrollview and I get the keyboard. When I delete the UIScrollView, my method (show picker) is called and I get my picker.
In case you are already using the textfield delegate, try ...
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[self openCustomPicker:self]; // Call your IBAction method
return NO; // Hide both keyboard and blinking cursor.
}
to prevent the UITextField from showing the keyboard.

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>