(I'm targetting iOS 2.0 for the time being.)
I have a class which subclasses UITextField and implements the UITextFieldDelegate protocol.
At first, I did not set the class as its own delegate, and when I clicked in the field, the keyboard popped up automatically. So far, so good.
I send the setReturnKeyType: message with UIReturnKeyDone, so it changes the bottom-right button to say Done.
The problem was, when I clicked Done or pressed [Return], nothing happened - the keyboard wouldn't go away.
I tried adding self as an observer for the end editing notification, but it never got called when Done was clicked; from a Google search it seems that only gets fired when the field resigns as the first responder - which is the bit I can't get to happen.
I then found answers on here that suggested adding self as the delegate and handling textFieldShouldReturn:. The problem is, as soon as I add self as the delegate, the keyboard no longer pops up when you click the field (and it doesn't gain focus) - it seems it isn't becoming first responder.
I tried handling textFieldShouldBeginEditing: and returning YES (docs say that should be the default if not overridden), and that got called, but made no difference. In my ignorance I tried [textField becomeFirstResponder] and was rewarded with a stack overflow (now I know that is called in response to trying to become first responder).
I'm now thoroughly stuck!
Can anyone help? I just want the keyboard to go away when the user clicks Done or presses [Return].
Are you using an xib or doing it programatically? If you are using an xib then you may have forgotten to connect the delegate in File's Owner.
(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Try This one.
My fault. Turns out you're not supposed to set a text field's delegate to itself - there are complications due to unhandled selectors being forwarded to its delegate. If it doesn't respond to a selector the message goes to its delegate... which is itself, which we already know doesn't respond. Hence, an infinite loop.
Using a separate object as the delegate worked perfectly.
Hopefully this will help someone else avoid the same problem.
Related
I'm facing a strange problem with dealing with UITextFields and keyboard visibility.
The purpose of my current controller is to let user edit some datas but also read some others.
So I decided to include few textfields in it and when user modify some data a button appears and he can press it to send modifications. But to read other datas keyboard has to hide.
I made some researches to find a working way to do so like :
self.view.endEditing(true)
currentTextField.resignFirstResponder()
even create an "hidden UITextField" and ask it to becomeFirstResponder when process is done
None of these tries ended with a hide of the keyboard but ended with Optional value crash
I'm using Swift 3 and XCode 8.2.1 right now
If someone has a clue, thanks by advance
Your view controller needs to subclass UITextFieldDelegate. Implement the functions that are defined by this protocol and you should have the control you need. And don't forget to set myTextfield.delegate = self -- or whatever object implements the protocol.
An example would be "DidEndEditing" and similar.
If you already did this please provide some code for further diagnosing your issue.
Thanks for your answers,
I implemented UITextFieldDelegate from begin of code and all my TextFields use editingDidBegin and editingDidEnd and crash occured when I called the code below from the result of an API Call
self.view.endEditing(true)
I just found a way to succeed the hide of keyboard using this code before the API call. The sad side is that this new way avoid the possibility to keep keyboard visible if the call fails.
self.view.endEditing(true)
This line should not crash. It just resign responder from UITextfield. Can you please give me detail about your crash? What is written in log console when crash happens. as far as I know self.view.endEditing(true) will definitely fulfil your requirements.
I added a UITextField to my storyboard:
#property (weak, nonatomic) IBOutlet UITextField *nameField;
And it was working fine at first, but had some constraint issues which caused the view to not build correctly on an iPhone 5. So I messed with some of the constraints to get it to display correctly and now the keyboard never comes up/the UITextField refuses to respond to touches OR become active.
I tried setting a UITapGestureRecognizer to the nameField, didn't work.
I made sure there were no views blocking it (checked on the view debugger, and then explicitly called [self.view bringSubviewToFront:self.nameField]; in viewDidAppear)
I tried [self.nameField setUserInteractionEnabled:YES]; as well because why not.
So then I went to the storyboard, clicked on the text field and under the connection tab on the right side menu I looked at "Sent Events" and hooked up Touch Up Inside to an IBAction method:
- (IBAction)beginNameEdit:(id)sender {
NSLog(#"Edit begin");
BOOL accepted = [self.nameField becomeFirstResponder];
if (accepted) {
NSLog(#"accepted");
}
}
Nothing happens, (Edit begin did not print). (I touched up inside the boundary as well for sure, but whatever)
So then I hooked up Touch Down to the same exact IBAction.
This time, I started getting 'Edit begin' to print but the call to become first responder did nothing despite accepted actually getting printed (which means that according to Apple, the text field accepted first responder status)
I can't seem to make my textField active and so it's forever stuck on showing the placeholder text... It makes no sense, especially because I would be shocked if playing with some of the auto layout constraints could have any sort of effect.
Any suggestions?
Ok, and in classic the-answer-is-the-last-thing-you'd-expect fashion:
I was using the simulator to test the app. Turns out the keyboard doesn't always come up when you're using the simulator because it auto-detects the fact you have an ACTUAL keyboard on the computer you are using. So instead of a keyboard coming up on the simulator it was silently becoming the first responder and waiting for me to type something on my computer's keyboard.
Why the keyboard was coming up earlier and not now, I have no clue. But I'll leave this up in case anyone else comes across this issue.
I'm trying to work with a textview but its having odd reactions. The textview is where the user enters their information that will be sent to me. Basically I've designed my interface in interface builder and got it looking correct. I have entered place holder text: "Enter your issue here". When I run the app the textview works correctly, I can delete the pre entered text and compose a message. However I need to place a ui tool bar above the key board so I can give the user a way of canceling input. i've implemented its delegate, UITextViewDelegate and implemented
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{}
which does get fired when I tap the text view. However the textview now does not give me the keyboard! it just sits there awaiting input but never displays the keyboard. I need to use textViewShouldBeginEditing so I can tell when the user touches the text view to activate the text entry and attach the uitoolbar I. If I remove the textViewShouldBeginEditing the text view goes back to responding being tapped. I've tried a good few thing, ensured the right connections are made in interface builder, set the delegates etc, etc but nothing seams to fix this.
Any suggestions are welcome and if any more information is needed let me know.
Many thanks in advance.
Are you returning TRUE in the delegate?
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
//Do your things
return TRUE;
}
you can try this
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(myMethod)
name:UITextFieldTextDidChangeNotification
object:myTextField];
you can use UITextFieldTextDidBeginEditingNotification or UITextFieldTextDidEndEditingNotification
I have been looking for a similar questions but so far the answer doesn't fit my purposes. I have a UITableViewController inside A UINavigationController with custom UITableViewCells containing textfields inside. When I click on the textfields they become the firstResponders and when I click return they resign it. So far so good.
My problem is one of these cells is performs a Submit function. After this is done whenever I press the textfields the return button doesn't dismiss the keyboard anymore.
My question is, since I'm not releasing anything, why do these textfields stop listening to the resignfirstresponder message?
UPDATE: I finally got this working by setting an UITextfield ivar in the class, making it the first responder whenever the textfield begins editing in :
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField setTextColor:[UIColor blueColor]];
focusedTextField = textField;
}
And calling [focusedTextField endEditing:YES]; when i press the submit button. Just added this after seeing some answer in SO, but I can't remember the link, sorry.
Hope it helps.
If you have declared a method for return(let's say your textfiled is called textField) use this code in your method;
[textField setUserInteractionEnabled:NO];//but this may restric you if you need to use it again
But if you use standard return property of the keyboard it may be something related to Apple's restrictions
I'm experiencing some problems with UITextField inside a UITableViewCell.
Things are working well until I open the iOS media player, watch a short movie and going back to my view.
Then, I'm trying to click on the UITextField, but nothing happens and it does not responds to my clicks.
In the same screen (it's a UITableView), I have a switch button (in another row), which is working fine after switching views.
My view is implementing the UITextFieldDelegate protocol and textFieldShouldReturn in particular.
My implementation of textFieldShouldReturn is a simple call to : [textField resingFirstResponder]
I'll appriciate any thoughts or ideas why it happens and how to solve it.
Thanks!
koby
I had this same problem, what fixed it for me was to make sure to return the correct height for the table view cell. Check your heightForRowAtIndexPath function - even though you can see all the objects in your cell they may be outside of a clickable/tappable area.