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.
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;
}
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.
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.
I am working on an iPad app that has a login controller which segues into a view controller. The login controller is declared thusly:
#interface LoginController : UIViewController <UITextFieldDelegate>
and the storyboard has (as expected), a username and password text field and a button for authentication and logging into the main app. The button calls the shouldPerformSegueWithIdentifier(...) function and the view switches from the login view to the main view.
I would like to also mimic this programmatically, when the user hits return on the password text box. I've trapped the event, but I can't seem to get the switchover to happen. The code I'm using is:
if (theTextField == self.password)
{
BOOL loginSuccessful = [self shouldPerformSegueWithIdentifier:#"switchToViewer" sender:self];
if (loginSuccessful == YES)
{
[self dismissViewControllerAnimated:YES completion:^{
NSLog(#"I should be dismissing here!\n");
}];
}
}
However, the view never gets dismissed. I should note that this is on iOS 7, I don't know if that matters. Any ideas?
EDIT: My workaround for now is to spoof the button touch event:
[self.signIn sendActionsForControlEvents: UIControlEventTouchUpInside];
Hacky, but it works :)
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
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.