hide when push view controller - ios

When I tap the go button on the keyboard I push to a new view controller, but for a split second on the new view controller the old keyboard is still showing, how can I resign the keyboard so it doesn't appear on the new view controller I have tried both
[self.view endEditing:YES];
And this
[_passwordField resignFirstResponder];
in the prepareForSegue method and the same two lines in the IBAction

I think you have first tying to push to a new view controller, after that you are code for hide keyboard. Use following code for your problem, use this delegate method textFieldShouldReturn.
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
if ( textField == _passwordField){
[_passwordField resignFirstResponder];
//After that Push to a new view controller From Here
}
return TRUE;
}

Related

Not able to dismiss the keyboard on textfield

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

KeyBoard gets locked after popover dismissal

There are multiple textfields in a viewcontroller in which some of them are customised (one tapping those textfield will launch a popover controller, from that user can select the option which will get displayed in tapped textfield).
I have a tap gesture on the view controller for dismissing the keyboard (if it's on the screen).
Keypad gets locked(if it's visible) when I open the popover controller on taping the customised textfield. The keyboard is not getting dismissed even if I tap on the parent view or else on the dismiss button in the keypad.
I have tried this 2 snippets to hide the keyboard, but it's not working
[self.scrollView endEditing:YES];
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
You can use textfields delegate to prevent it from presenting a keyboard and instead present popover yourself by implementing this textFieldShouldBeginEditing method
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if(textField == myCustomTextField) {
[self openCustomPopover];
return NO;
}
return YES;
}
more on its delegate methods here https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextFieldDelegate_Protocol/index.html#//apple_ref/occ/intfm/UITextFieldDelegate/textFieldShouldBeginEditing:

keyboard is not dismissing if i use modal segue

I am using simple Modal segues between view controllers and i it is not dismissing the keyboard. I tries [textField resignFirstResponder] on view will disappear method but still no luck.
I am not using any Navigation Controllers, just simple View Controllers.
I also tried doing this but still no luck. Need Help.
View Controller A ---------> View Controller B
in View controller B. I called [self disablesAutomaticKeyboardDismissal] in viewDidLoad.
and I've overridden in it like so
-(BOOl)disablesAutomaticKeyboardDismissal
{
return NO;
}
This one could help you too
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
}
With this, when you tap outside the textfield , the keyboard disappear

Resign First Responder on Push Segue

I have a push segue and want to transfer data between views. I need a different way to resign first responder if the user taps the next button in the navigation bar. I have [textField resignFirstResponder]; in the navigation bar button method but for some reason it is called after the next page viewDidLoad.
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:[NSString stringWithFormat:#"thridClubPage"]]) {
thirdClubPage *nextPage = segue.destinationViewController;
nextPage.title = self.navigationItem.title;
nextPage.location = self.collegeString;
nextPage.people = self.membersString;
nextPage.leader = self.presidentString;
nextPage.Email = self.presidentEmailString;
}
}
- (IBAction)finishPage:(id)sender {
[self.presCell.presidentEmail resignFirstResponder];
}
the segue performs without resigning the textField.
A quick solution is to delete the segue from the button in IB and redraw it between the two view controllers. (In other words, when dragging out the segue, don't control-drag from the button, control-drag from the view controller that contains it). Give the segue an identifier, like #"NextSegue".
Now you can control the order of what happens by triggering the segue yourself...
- (IBAction)finishPage:(id)sender {
[self.presCell.presidentEmail resignFirstResponder];
[self performSegueWithIdentifier:#"NextSegue" sender:self];
}

UISearchbar Keyboard not dismissing

I have a view controller whose view is setup in such a way that it has 3 buttons and other subviews. On clicking one of the buttons(3rd button), i am adding another view controller's view as subview to self.view (in this view i have a search display controller in the active state with keyboard)
I'm able to achieve this using the following code
[self.searchDisplayController setActive:YES];
[self.searchDisplayController.searchBar setShowsCancelButton:NO];
[self.searchDisplayController.searchBar becomeFirstResponder];
Now when i press the 2nd button, i try removing this view from the superview and also try to resign the keyboard in the viewWillDisappear ([self.view endEditing:YES]) in the following manner, but keyboard still doesnt resign
One small edit, it is resigning in case i comment out the following piece of code
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
if (self.dataSource.count)
return YES;
return NO;
}
Try to resign UISearchBar in viewWillDisappear
[yourSearchBar resignFirstResponder];
Inside the method that is called to your 2nd button, just type in
[self.searchDisplayController.searchBar resignFirstResponder];
Leave the viewWillDisappear alone. That is for when the view is done with whatever animation it is doing, and when the view is leaving. If you set the display controller to resigning the responder, it should make it go away immediately.
Hope this helps!
you can do it using NSNotificationCenter as below.
//.m file:
[[NSNotificationCenter defaultCenter]addObserver:self
selector:#selector(ResignFirstResponder:) name:#"resign" object:nil];
in ResignFirstResponder:
[searchBar resignFirstResponder];
in which class you use above addObserver, you have to implement that method.
//.m file from where you want to send action, call method as below. [on your second button click]
[[NSNotificationCenter defaultCenter] postNotificationName:#"resign"
object:nil];

Resources