I have a modal View Controller, presented as Form Sheet on the iPad.
When I send [textField resignFirstResponder], the Keyboard remains on the screen.
In the View Controller:
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
In the Navigation Controller:
- (BOOL)disablesAutomaticKeyboardDismissal
{
return NO;
}
All this worked using iOS 6, but not with iOS 7.
Adding the following method to the actual ViewController (rather than the NavigationController) worked for me in iOS 7.
- (BOOL)disablesAutomaticKeyboardDismissal
{
return NO;
}
I'm calling a method that is hooked to the Text Field's Sent Event Editing Did End.
- (IBAction)KeyboardDoneKeyPressed:(id)sender
{
[sender resignFirstResponder];
}
Prior to adding the method disablesAutomaticKeyboardDismissal the keyboard would not dismiss when pressing Done.
Related
My view controller structure on story board.
Main View -> scroll view -> view -> 3 text field(On main view add one scroll view, again add one view and than add 3 text field).
On 1st textfield(Date), open date picker.
on 2nd textfield(city), open new view controller for city list and after selecting city it will open new view controller for work area.After selecting work area it will come back to main view controller and selected city and work area is display in second text(city) field.
on 3rd textfield (Customer), open normal keyboard.
It is working fine but the problem is if I click on customer textfield, it will open keyboard at same time I click on city textfield than it will open city list View Controller but keyboard is not dismiss. keyboard is also show on city list view controller.
I have already try below thinks.
I am already trying with [self.view endEditing:YES] and resignFirstResponder. But not getting any successful result.
I have assign the tag to textfield And also set delegate in viewDidLoad() Method.
I have also assign the delegate from storyboard.
self.cityOrAreaTXTFld.delegate = self;
self.selectCustTXTFld.delegate = self;
self.dateTXTFld.tag = 1;
self.cityOrAreaTXTFld.tag = 2;
self.selectCustTXTFld.tag = 3;
My textFieldDidBeginEditing()Method
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if(textField.tag==1){
NSLog(#"Code to open date picker");
}
else if (textField.tag==2){
[self.view endEditing:YES];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *CityPEVC = [storyboard instantiateViewControllerWithIdentifier:#"CityListViewController"];
[self.navigationController pushViewController:CityPEVC animated:YES];
return;
}
else if(textField.tag==3)
{
[_dateTXTFld resignFirstResponder];
[_cityOrAreaTXTFld resignFirstResponder];
}
}
I have one solution for this
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField.tag == 2)
{
[_dateTXTFld resignFirstResponder];
[_selectCustTXTFld resignFirstResponder];
return YES;
}
else
return YES;
}
In this solution, If key board is not open than not an issue but if any keyboard is open on main view controller and First time click on city textfield it will dismiss the keyboard(not open city list VC) and second time click on city textfield that time it will open city list view controller.
Is there any solution, if any other textfield keyboard is open on main view controller, and I click on city list textfield. Than how to dismiss that keyboard and same time open city list view controller?
I guess it would make sense to use it inside viewWillDisappear: in order to hide the keyboard before the controller is dismissed
[self.view endEditing:YES];
Apply this method on TouchesBegan function
sample code is below:
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
}
When you touch any where in screen it will dismiss keyboard.
viewController.h
#interface yourViewcontroller : UIViewController
<
UITextFieldDelegate
>
viewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
yourTextField.delegate=self;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self.view endEditing:YES];//dismiss all text field
return YES;
}
In my application when the TextField loses focus I am checking the values as follows.
- (void)textFieldDidEndEditing:(UITextField *)textField {
if (textField.tag==1){
[self CheckUser:textField.text];
} else if (textField.tag==2){
[self CheckEmail:textField.text];
} else {
}
}
It is working fine, no problem in it.
But when I dismiss the Modal form and move to the ViewController, it is crashing. I am using the below code for dismissing the Modal View Controller.
[self dismissModalViewControllerAnimated:YES];
If I remove the code - (void)textFieldDidEndEditing:(UITextField *)textField it is working perfectly without crash.
Can anyone help me.
Make sure you're setting the UITextField delegate in the text field's header file:
#interface ViewController : UIViewController <UITextFieldDelegate>
Afterwards, release the isfirstResponder property of the text field. Use the code below as an example.
#pragma mark - UITextField Delegate Methods
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(#"Search for tag: %#", self.searchField.text);
if (textField == self.textField1)
{
[self.searchField1 resignFirstResponder];
NSLog(#"TextField1: %#", self.textField1.text);
}
else if (textField == self.textField2)
{
[self.searchField2 resignFirstResponder];
NSLog(#"TextField2: %#", self.textField2.text);
}
return YES;
}
Edit
Also, if your view hierarchy is embedded in a UINavigation controller, dismiss the modal view being presented with:
// Apple's Description: Dismisses the view controller that was presented modally by the receiver.
[self dismissViewControllerAnimated:YES completion:nil];
This will remove the top most View Controller. ^^^ edit ^^^
Last thing, if it's still not working, set strong references to your TextField IBOutlets and set each of their .delegate properties in the viewDidLoad.
I've got a HomeViewController that has different modal segues to several other UIViewControllers. If I try to show the keyboard on a UITextField within the HomeView, everything works fine. However, if I try to show the keyboard on a UITextField (using becomeFirstResponder) after returning from any of the modal View Controllers, the keyboard never shows.
Here's some sample code from one of the setups I've tried:
In HomeViewController:
- (void)viewDidAppear:(BOOL)animated
{
static BOOL firstTimeComplete = false;
if (!firstTimeComplete) {
firstTimeComplete = true;
} else {
UITextField *textField = [[UITextField alloc] init];
[self.view addSubview:textField];
[textField performSelector:#selector(becomeFirstResponder) withObject:nil afterDelay:3]
}
}
In ModalViewController:
- (IBAction)done:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
Where done: is linked to the "Done" button via a touch up inside event.
A few things I've tried:
Converting the modal segues to push segues fixes the issue, but I don't want a Nav bar in any of the child views
I've tried disabling and enabling animations when dismissing the
modal view controller (using dismissViewControllerAnimated:)
Using unwind segues in the storyboard rather than doing it programmatically
Anyone have an idea of what may be going on?
After deleting tons of code, I finally found out that a custom NavigationController was being used and this was the root cause:
#implementation MSLNavigationController
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotate
{
return NO;
}
#end
The app doesn't need this code, so I've nuked the file. (But an explanation as to why this would be hiding the keyboard would be awesome :))
You did not call [super viewDidAppear:animated]
In place like that i have workaround that works pretty well
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^
{
if (self.textView.text.isNotEmpty)
{
[self.textView becomeFirstResponder];
}
});
}
I have been struggling with this problem for some time, so I'll post here what I found out.
I was calling textField.becomeFirstResponder() in viewWillAppear but on iOS 7, after the modal was dismissed, the keyboard would not show again, even when you would tap on the textField.
For me calling textField.resignFirstResponder() when the modal is presented, solved the issue. It seems like the input field was already marked as first responder and then would not react to the new calls.
My keyboard appears with a textView, I want to hide it when the user push on a back button on a navigation bar.
I have tried this:
-(void)viewWillDisappear:(BOOL)animated{
[myTextView resignFirstResponder];
}
and this:
-(void)viewDidDisappear:(BOOL)animated{
[myTextView resignFirstResponder];
}
But it doesn't work, how can I do this?
edit:
I found the solution here:
iPad keyboard will not dismiss if modal ViewController presentation style is UIModalPresentationFormSheet
Put this into the buttonPress method -
[self.view.window endEditing:YES];
Edit - this also lets you get the contents of the text being edited when the "back" button is pressed
Combining the above answers and checking for back button will be done by this
- (void)viewWillDisappear:(BOOL)animated{
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
// back button was pressed. We know this is true because self is no longer
// in the navigation stack.
[self.view.window endEditing:YES];
}
[super viewWillDisappear:animated];
}
I have UIView with few UITextFields. On load I call [textfield becomeFirstResponder] to bring keyboard on the screen. I don't want to dismiss this keyboard until I done with view.
Keyboard dissapear after I "touch" outside any textfield.
I tried to set
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
return NO;
}
however then I can't dismiss keyboard even after is unloaded.
Any hints how to keep keyboard all the time and dissmiss it just before uiview remove?
When viewWillDisappear (or viewDidDisappear) is called, then call resignFirstResponder on your UITextField.
- (void) viewWillDisappear:(BOOL) animated {
[self.textField resignFirstResponder];
}
call this line before dismiss.
view.endEditing(true)