I have a UIViewController that has two text fields, inputA and inputB. inputA works like a normal text field, click it and the keyboard shows up and you can type. But inputB triggers another view (think of it as a button). This second view, MyView, has a custom popup view which is animated to appear with the keyboard. MyView's popup is initially with the alpha to 0 and I have set notifications for when the keyboard shows up to have the popup's view alpha to be set to 1.
This works great, but I have a "bug" where when the keyboard is currently showing because the user tapped inputA first, then they tap inputB, it fires the MyView but because the keyboard is already showing the notification isn't sent to change the popup alpha to 1.
So the question: what is the clean way to work around this issue? I want the view to appear along with the keyboard, but if it is already there, it should just be sent.
Is there a way to test if the keyboard is already showing? I want to do this without setting a bool value every time the keyboard is shown/hide in those two views.
try the following
if ([inputA isFirstResponder] || [inputB isFirstResponder]){
//keyboard is displayed
}else{
//keyboard is not displayed
}
Related
I have a UIScrollView with 2 views, side by side, each of which covers the entire screen.
They are moved to visible bounds on user's action, only one covering the screen at a time. Both of these views have multiple UITextFields. Working with the simulator, I fill in a textField in the first view and when I press the Tab key, the firstResponder is assigned to a textField in the other view. I understand that on using the device, the user will not be able to do that. But what if the user uses a bluetooth keyboard, or similar accessory? I do not want a textField, that is currently not visible to become firstResponder. Can this be done?
EDIT: I just remembered the canBecomeFirstResponder method. But how do I determine which textField is about to becomeFirstResponder?
It sounds like the problem isn't that they shouldn't be able to tab between the two text fields, but instead that they shouldn't be able to edit a text field that isn't visible, and they should be able to tab between them if they are both visible at the same time.
Instead of restricting tab, I would implement the UITextField delegate method -textFieldShouldBeginEditing:, which allows you to return a boolean whether or not that text field should become the first responder.
Something such as:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
// Only edit if the text field is visible
return !textField.isHidden;
}
You may need to adjust this code to fit your 'is visible' status of the text field.
When I tap UIWebView, the keyboard will disappear, This is not What I want , I want the keyboard remain there, any solution?
If you want your keyboard to disappear when you remove your text view, make sure to call resignFirstResponder on your text view before you remove / release your text view.
If you are touching the UIWebView while the text view is visible, then the focus changes from the text view to the web view and the keyboard dismisses, since the OS believes that any touches happening from that point are meant for the web view. If you touch the text view (if it's still visible, that is), the focus changes back to the text view and the keyboard should re-appear.
If these answers don't help you, please clarify your question.
I have a table view containing 10 cells and each cell has a text field. Till textfield 5, I want the user to enter some value using keyboard. On text field 6, I want the user to select values from a list (showing picker view).
What is happening is when I click on field 5 (showing keyboard), entered some value and then hit return button (on keyboard), the keyboard goes down and then, I click on text field 6 (showing picker), the picker is shown (no keyboard appears here).
BUT if I dont hit return button of keyboard (on field 5) and directly click on field 6 (picker), then my picker appears with the keyboard at the top, that is, keyboard doesn't goes down and picker appears behind the keyboard. Here, when I click on return of keyboard, then also keyboard doesn't goes down. To make the keyboard go down, I need to click on any text field (showing keyboard) and then hit return.
Has anybody faced this strange problem?? Please help me out.
you have to set the tag of each textfield and set the delegate and then put this delegate method like this,
(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
If (textField.tag == 6)
{
[textField resignFirstResponder];
}
return YES;
}
Enjoy!
Dont add the picker as a subview manually. Set it as the inputView of the text field (on cellForRowAtIndexPath) and the OS will take care of showing and hiding for you.
In my iPhone APP I have a view with a textfield and a button (to remove the view).
My main goal is to have the keyboard always visible.
I made a "Send" button visible on the keyboard and am able to capture when the send button is pressed.
Hoever, when you press the send button the keyboard is removed. What I would like is for the keyboard to remain visible and the text from the textfield to be cleared and be textfield to have focus ready for some more typing.
Adding:
[textChat becomeFirstResponder];
in the Did End On Exit event does not work. I am not sure if I should be using one of the other events.
There are a ton of samples and tutorials on how to remove the keyboard, not one on how to keep it.
The keyboard is removed when it stops being the first responder. There is a delegate method you can implement, textFieldShouldEndEditing:, which is called when the text field is asked to resign from being first responder. You could implement this and have it return NO after you do whatever you want to do with the data in the text field (send it somewhere), clear the field, etc.
I have a modal window that's used for searching data from a remote server- it has a UITextField as the titleControl of the navbar for the window, and a tableview filling the window (that displays the results obviously). Now what I want to do is when the user scrolls the tableview, immediately have the textfield lose focus (resign first responder) so that the keyboard dismisses and the user has more room to scroll through the tableview (it stretches down to fill the gap left by the keyboard). Basically the same functionality as when using a UISearchDisplayController (or whatever it's called).
So I have this code for detecting the scroll event of the tableview:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[searchField resignFirstResponder];
}
Which works fine. However, the issue is that once the user scrolls the table and the textfield loses focus, you can't give focus back to it by tapping on it again. So basically once I call that [resignFirstResponser] I can never again bring the keyboard back up and edit the textfield value. Anyone have any idea why? Do I need to explicitly call [becomeFirstResponder] on the field somewhere? Because I thought that was handled automatically when the field is tapped?
Also of note- I am calling [becomeFirstResponder] on the text field right when the modal window is first called up, so the field is pre-focused. Could that have anything to do with it?
I can post more code if anyone would like, but I don't think I'm doing anything out of the ordinary with the textfield.
Thanks for any help!
You are calling the resignFirstResponder from a function which will be called everytime you scroll the UIScrollview. Hence it does not appear. You need to call resign when the uitextview goes out of focus.
You can do the following. Its a hack:
Whenever you focus on the UITextField create a invisible button to overlay your scroll view.
Capture the button press event and resign first responder
Whenever the uitextfield becomes first responder create the button
This way you will remove the bug, viz calling the method in scrollViewWillBeginDragging.
Other option would be to overrite viewDidAppear method for the uiTextField.
Or you could put your textfield into a different container and handle scrollViewWillBeginDragging by checking which scrollview sent the message.
Did u set a delegate for you searchField? I had the same issue. I popup a model view, and set the text field to be the first responder inside viewDidLoad. Everything works well for the first time. But once I dismiss the modal view controller, and reopen it. my text field cannot be focused anymore.
I found it has something to do with methods of UITextFieldDelegate. Once I remove implementation for methods
– textFieldShouldEndEditing:
– textFieldDidEndEditing:
everything works well. but don't know why
Are you doing anything with "textFieldShouldEndEditing", like #fengd?
A problem that I had was that I was viewing a modal view, and my "textFieldShouldEndEditing" routine was incorrectly returning "NO" on a specific text field. When my modal got dismissed, I would be unable to tap on any other text-field, presumably because the old text field was still "first responder". Since it can never end editing, it fouls up all other text fields that come after it.
I realize this is 2 yrs after the fact, but maybe someone else might find this useful.