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;
}
Related
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;
}
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.
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:
I have two tableviews, let´s say one and two, and one presents two when a textfield is tapped, and for this i have:
-(void) setProducts:(UITextField *) box
{
UIViewController *selectInBox = [self.storyboard instantiateViewControllerWithIdentifier:#"selectingProducts"];
popover = [[UIPopoverController alloc] initWithContentViewController:selectInBox];
[popover presentPopoverFromRect:box.bounds inView:box permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *) textField
{
if (textField.tag == 2)
{
[self setProducts:textField];
}
Everything works great until know, table "two" appears inside the popover and underneath the textfield, but table "two" has a cancel button that it is supposed to dismiss tableview "two"(inside tableview "one"). So i went to my cancel method in tableview "two" and i did this but it isn´t working and i don't think it can be simple has this:
- (IBAction)cancel:(id)sender
{
[self removeFromParentViewController];
}
I also tried:
- (IBAction)cancel:(id)sender
{
one *aux = [[one alloc]init];
[aux.popover dismissPopoverAnimated:YES] //popover is a property of tableview one.
}
also tried: "if self isBeingPresented"...and nothing is working!.
Any ideas? help!
thanks in advance
I've used this quite successfully with "popovers" that are presented as form sheets (modally).
- (IBAction)cancelButtonTapped:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
The dismissPopoverAnimated:YES... should be invoked from the presenting view controller.
I have a UISearchBar that, even though it is the first responder, the keyboard is not shown. I can start typing in the search bar using the laptop's keyboard, and I can see what I type inside the search bar, but the keyboard in the Simulator and on the actual iPad does not appear. It worked fine under iOS 3.2, but stopped working after updating to iOS 4.2 this morning.
Below is the relevant code:
// Text Field that when touched will fire a search view that contains the search bar
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[textField resignFirstResponder];
UIViewController *detailViewController = nil;
ImplementedSearchViewController *searchController =
[[ImplementedSearchViewController alloc] initWithNibName:#"ImplementedSearchView" bundle:nil];
...
detailViewController = searchController;
MyAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
UINavigationController *nav = (UINavigationController *)
[delegate.splitViewController.viewControllers objectAtIndex: 0];
NSArray *viewControllers = [NSArray arrayWithObjects:nav,
detailViewController, nil];
self.splitViewController.viewControllers = viewControllers;
HomeViewController *hHomeController = [nav.viewControllers objectAtIndex:0];
// Dismiss the popover if it's present.
if (homeController.popoverController != nil) {
[homeController.popoverController dismissPopoverAnimated:YES];
}
// Configure the new view controller's popover button
(after the view has been displayed and its toolbar/navigation bar has been created).
if (homeController.rootPopoverButtonItem != nil) {
[detailViewController
showRootPopoverButtonItem:homeController.rootPopoverButtonItem];
}
[detailViewController release];
}
// Inside the viewDidLoad of the search view
- (void)viewDidLoad {
self.table.frame = CGRectMake(table.frame.origin.x, table.frame.origin.y, table.frame.size.width, 680);
self.table.backgroundColor = [UIColor colorWithRed:239/255.0 green:244/255.0 blue:255/255.0 alpha:1.0];
// searchBar is a UISearchBar
[self.searchBar becomeFirstResponder];
}
The ui search bar in viewDidLoad is not nil.
Any thoughts on this?
Thanks,
Mihai
I was showing searchbar on textFieldDidBeginEditing:(UITextField *)textField delegate method.
Keyboard was not showing. So for that first resign textField as firstResponder.
i.e.
[textField resignFirstResponder];
Then call method with delay
[self performSelector:#selector(callSearchBar) withObject:NULL afterDelay:0.2];
Where
-(void)callSearchBar{
[self.searchDisplayController setActive: YES animated: YES];
self.searchDisplayController.searchBar.hidden = NO;
[self.searchDisplayController.searchBar becomeFirstResponder];
}
It works
I am using the same ui search bar in different places. It works in all but two. I believe that the problem has to do with the text field not resigning as first responder (although I called resignFirstResponder on that text field). In one of the places that it works, I'm also using a text field to trigger the search bar, but I'm using a selector instead of a text field delegate method (such as textFieldDidBeginEditing). If I switch from using a selector to using the delegate method, it stops working and I get the same issue (keyboard not appearing). I thought that, for the two cases where it doesn't work, the solution would be to switch from using the delegate method to using a selector, but it didn't. What I ended up doing was switching from using a text field to using a button. In a way, it doesn't make any sense using a text field if you are not going to write anything in it (I was only using it to trigger the search bar).
I can verify, that resigning the first responder in favor of the searchbar solves the problem on an iPad under iOS 4.3. In my case I had an UIViewController responding to UIEventSubtypeMotionShake. You had to explicitely call resignFirstResponder before(!)
removing thie UIViewController from the view hierarchy to make it work.
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (motion == UIEventSubtypeMotionShake){
...
}
}
...
[someViewController becomeFirstResponder];
...
[someViewController resignFirstResponder];
[[someViewController view ] removeFromSuperview];
... (in my case I added a hierarchy including an UITabBarController, with a Tab including a UINavigationController covering the Searchcontroller to the window instead)
[searchViewController. searchbar becomeFirstResponder];
No clue, why this was not necessary on iPhone.
[self.searchDisplayController setActive: YES animated: YES];
self.searchDisplayController.searchBar.hidden = NO;
[self.searchDisplayController.searchBar becomeFirstResponder];
This answer from Mann(https://stackoverflow.com/users/871079/mann) worked for me. Thank you very much.