I am currently programming an application for iOS 7, but I have recently come across an interesting error. In one of my UIViews, I have 3 normal UITextFields and two other UITextFields that resignFirstResponder when editing begins. They show a UIDatePicker and UIPickerView as well as a UIToolbar. The error I am having is when I am typing in one of the first 3 text fields, and proceed to click the textfield that ends editing without clicking the UIControl (which is called backgroundTapped:), the keyboard does not disappear. I added a log to see whether or not the text field is resigning the firstResponder status with "canResignFirstResponder", and it is returning "1", yet the keyboard does not disappear, even when changing views until I click one of the top 3 text fields and click the background.
Here is my textFieldDidBeginEditing: and the start of my showRunTypePicker: methods:
textFieldDidBeginEditing:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField becomeFirstResponder];
if (textField.tag == 3005) {
//[textField resignFirstResponder];
//[self.view endEditing:YES];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.25];
[self showRunTypePicker:self];
[UIView commitAnimations];
}
}
showRunTypePicker:
- (IBAction)showRunTypePicker:(id)sender
{
BOOL canResign = [runTypeField canResignFirstResponder];
NSLog(#"canResign: %hhd", canResign);
[runTypeField endEditing:YES];
[runTypeField resignFirstResponder];
[[self view] endEditing:YES];
[pickerView endEditing:YES];
[pickerView setHidden:YES];
[toolbar setHidden:YES];
[distanceField endEditing:YES];
...
}
I cannot seem to figure out what the problem is. Can anyone help me on this?
EDIT: It's working now. I set the [self showRunTypePicker:self] to [self showRunTypePicker:textField] and moved it to textFieldShouldBeginEditing. Now the keyboard properly disappears.
You have a bug in code.
Read this line
[self showRunTypePicker:self];
Don't you think it should actually be:
[self showRunTypePicker:textField];
Here your intention is to resign keyboard when you tab specific text field .
So you should be actually passing the
textField
instance whose tag is 3005.
Try to use sender as a UITextField and perform your bool check.
Hope that helps.
Related
I have created 5 textFields.
And, create tap in tableView to hide keyboard of textfield
UITapGestureRecognizer *tapTableView = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapTable)];
[_tableView addGestureRecognizer:tapTableView];
-(void)tapTable{
[txt1 resignFirstResponder];
[txt2 resignFirstResponder];
[txt3 resignFirstResponder];
[txt4 resignFirstResponder];
[txt5 resignFirstResponder];
}
But, it's not hide keyboard, it's called tapTable but not hide keyboard.
How can I resolved this problem!
It is happening due to you didn't set your IBOutlet and write more preety code as given below. Don't need to resign each and every text field.
-(void)tapTable{
[self.view endEditing:YES];
}
You don't need to do that.
Just use
[self.view endEditing:YES];
I'm implementing a share extension for my app, so far everything is going good except I can't seem to dismiss the keyboard that automatically opens using the default layout/storyboard.
I'm keeping the default design/layout (SLComposeServiceViewController) which includes the preview image and UITextview, the UITextview automatically gets in focus which opens the keyboard.
Normally this is fine, but if you're not logged in my app I display an UIAlertController saying you need to login to share. The problem is the keyboard opens at the same time as the alert.
I've tried [self.view endEditing:YES]; and [self.textView resignFirstResponder]; in both viewDidLoad, viewDidAppear and viewWillAppear with no luck.
Found the answer! I didn't read the docs very carefully...
I had to do [self.textView resignFirstResponder]; in -(void)presentationAnimationDidFinish
my way is to use UITextViewDelegate
- (void)viewDidLoad {
[super viewDidLoad];
self.textView.delegate = self;
self.canShare = NO;
[self.view setAlpha:0.0];
}
change canShare to YES in your check login logic
- (void)checkLoggedIn {
if ([[ShareAccountManager checkLoggedIn]) {
self.canShare = YES;
[self.view setAlpha:1.0];
}
}
and implement method textViewShouldBeginEditing
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
if (self.canShare) {
return YES;
}
return NO;
}
In my app I have a view that is a form that has quite a few inputs.
When the UITextField calls textFieldDidBeginEditing, it checks the tag and will bring up a UIPopoverController or the keyboard depending on the what the input is meant to be.
If the keyboard is up, I need it disappear when the user presses a textfield that brings up the popover. However I cannot make it disappear, I have tried every way to get rid of the keyboard but it just stays there. I have tried:
calling resignFirstResponder in textFieldDidEndEditing
calling [self.view endEditing:YES] in textFieldDidEndEditing
calling resignFirstResponder AND [self.view endEditing:YES] in textFieldDidBeginEditing checking for the previous tag is equal to a keyboard input text field.
Any ideas would be great.
I have ripped it out and and put it in a example project if anyone wants to see the exact behaviour.
http://dl.dropbox.com/u/61692457/KB_Test.zip
Declare a Global UITextField in .h file
UITextField *txtfld;
Replace Your method textFieldDidBeginEditing with textFieldShouldBeginEditing and now write this code
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField.tag == 1 || textField.tag==3)
{
if(numPickerPopover == nil)
{
numPicker = [[[NumPicker alloc] initWithStyle:UITableViewStylePlain] autorelease];
numPicker.delegate = self;
numPickerPopover = [[UIPopoverController alloc] initWithContentViewController:numPicker];
[numPickerPopover setPopoverContentSize:CGSizeMake(60.0, 260.0f)];
}
[numPickerPopover presentPopoverFromRect:textField.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[txtfld resignFirstResponder];
return NO;
}
if (textField.tag == 2)
{
txtfld = textField;
return YES;
}
return YES;
}
To dismiss the keyboard when the user touches the textField that brought it up, add this method:
- (IBAction)dismissKeyboard:(id)sender {
[textField resignFirstResponder];
}
In Interface Builder, connect this method to the textField event you want, like touch up inside (or whatever is more appropriate).
I have a strange problem with the iOS Keyboard.
In my app, I am using UITextFields inside some UITableViewCells. I want to dismiss the keyboard if the current textfield loses its focus.
This is what I've done so far:
Set up the <UITextFieldDelegate> and add [textField resignFirstResponder] to textFieldDidEndEditing:
-> textFieldDidEndEditing gets called, but the keyboard stays.
Added all TextFields to an array, looped through all objects and call resignFirstResponder
-> No effect
Called [self.tblView endEditing:YES] inside textFieldDidEndEditing.
-> Keyboard didn't disappear.
But dismissing the keyboard by using the Done-Button works perfectly (using textFieldShouldReturn)
What am I doing wrong?
Edit: I've made a video of my problem: http://www.youtube.com/watch?v=Zuz5rCv2GCo
try implementing following:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField becomeFirstResponder];
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
[self resignFirstResponder];
}
I have set the delegate of my textfield to self and I have added the delegate for it in the .h, but I have a problem. I want the keyboard to hide If I click anything but the textfield in the view. Is this possible? If so, how would I do it?
I found a simpler way to hide the keyboard and it works when you click on the screen way.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// endEditing: This method looks at the current view and its subview hierarchy for the text field that is currently the first responder.
// If it finds one, it asks that text field to resign as first responder
[[self view] endEditing:TRUE];
}
You can do some "hacking"... Like this:
- (void)textFieldDidBeginEditing:(UITextField *)textField{
//glass is a class's property
if(glass){
self.glass=nil;
}
glass=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; //put the size you want.
[glass addTarget:self action:#selector(hideGlass) forControlEvents:UIControlEventTouchUpInside];
[self.view insertSubview:glass belowSubview:textField];
}
-(void)hideGlass{
//remove your glass.
[glass removeFromSuperview];
//your textField resigns first responder.
if([myTextField canResignFirstResponder]){
[myTextField resignFirstResponder];
}
}
So basically what you do, is to add a dummy button right bellow your textField. So when you touch anything else, excepts your textField, he will make your textField resignFirstResponder and removes himself from the view.
Edit 1 ( the tweek) You just need to replace this:
if(glass){
self.glass=nil;
}
for this:
if(glass){
[glass release];
glass=nil;
}
You just need to implement the following delegate methods:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
If you have an IBOutlet for the UITextField, you can dismiss the keyboard using [textField resignFirstResponder];. This being said, you will have to implement event listeners for everything else on the view. If you want the keyboard to hide when the user taps the view's background, it can be done through a touch event in the view controller's implementation:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[textField resignFirstResponder];
}
If you have any buttons or subviews, you will have to implement individual touch events or actions for those as well.