简体   繁体   中英

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?

我想在viewWillDisappear中使用它是有意义的:为了在关闭控制器之前隐藏键盘

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM