I am developing a messaging application that have SMS.app-like UI. Conversation screen has text input field at the bottom which is moved up with keyboard. Tapping on conversation area dismisses keyboard by calling resignFirstResponder method on UITextView that we use.
This usually works fine, but users report a weird bug which I can't reproduce and fix. People say that sometimes onscreen keyboard doesn't go away when they tap on conversation area, though text input field loses input focus. Once the view enters that abnormal state users are able to type with keyboard, but text that is being entered is not visible anywhere. This bug happens in one of conversation views and since then no one text input field in other views doesn't work as expected. The only way to stop this weird behavior is killing application from multitasking bar.
Even more weird thing is that keyboard stays visible while navigating between view controllers in UINavigationController. I noticed two things:
if tap on '<Back' from conversation view controller with active text field and visible keyboard, keyboard goes away on view change.
if tap on '<Back' from conversation view controller with inactive text field and visible keyboard, keyboard doesn't go away on view change.
Anyone else experiencing same problems. Any ideas on what may cause this bug?
If the text field has a delegate, and the delegate is returning NO for textFieldShouldEndEditing, then resignFirstResponder will fail. I don’t know if that’s the issue you are seeing, but something to check.
set that textfield/textview delegate to fileowner.
Related
I have a storyboard, which includes navigation between views. Back and forth, and there is also the possibility to jump all the way to the home screen.
Basically, it works like this:
View 1 shows a simple menu.
View 2 shows input fields and automatically shows the keyboard.
View 3 shows a results table. In this view one can go one step back (to view 2) or back to the home screen (view 1).
The keyboard and everything works perfectly the first time, when the user moves from view 1 to view 2. However, when moving either back from view 3 to view 2, or from view 3 and starting all over again, the keyboard does not show.
In the code, I have becomeFirstResponder in the viewDidAppear method. Also, it will resignFirstResponder when loading view 3. Another interesting fact is that when calling the becomeFirstResponder for the second time, it does return NO (while it returns YES the first time). Also, when returning back to this view the textfields are not possible to select, even though they are userInteractionEnabled.
I am really stuck here, so any help is highly appreciated.
I have seen several similar questions to this one, but not found a solution:
becomeFirstResponder seems to work only 1st time for shake gesture
Adding [myUITextField becomeFirstResponder]; does not bring up keyboard
keyboard not responding to resignFirstResponder
UITextField becomeFirstResponder works only once
I was able to track down the problem in my code. Answering this question myself since it might be helpful to others as well.
My issue was that I didn’t really get the order of events correct. I had a button that would move to the next textfield, and if everything was completed it would move to the next screen. This caused the becomeFirstResponder method to be called and not released (resignFirstResponder) when switching to a new screen.
I guess the main advice I can give (at least what finally worked for me), was to just go through all the becomeFirstResponder and make sure it is released before moving to next screen.
I have a UIAlertView with a text field in it. This view works correctly; it appears in the top half of the screen, so when the keyboard comes up, both buttons and the text entry are still visible and tappable.
However, if a local or remote notification from another application comes in while the text field is the first responder and the keyboard is up, it hides my alert and places the new alert behind the keyboard. If the alert is more than one line long, its buttons cannot be clicked and the user cannot dismiss it. It's not dismissing my alert view via any of the normal mechanisms - neither alertView:clickedButtonAtIndex: or alertView:cancel: get called on my delegate - but rather simply hides it temporarily using some unknown mechanism.
Can I get notified when a notification hides my alert view, so I can dismiss the keyboard and let the user deal with the notification? Are there any other events I can hook into to catch this case?
Register for the application’s UIApplicationWillResignActiveNotification; when you get that, dismiss your alert view. From the docs:
An active application can be said to have focus. It gains focus after being launched, loses focus when an overlay window pops up or when the device is locked, and gains focus when the device is unlocked.
Alert views, such as those presented by notifications from other apps, are overlay windows. Watch for that notification, close your alert so the keyboard doesn’t get in the way, and be prepared to re-display the alert if your app then gets the UIApplicationDidBecomeActiveNotification.
I hope this will help...
- (void)willPresentAlertView:(UIAlertView *)alertView {
if (newAlertViewWhichBlocks.tag == alertView.tag) {
[yourTextField resignFirstResponder];
}
}
Do remember to set delegate of alert view.
I'm writing a universal iOS app that a text entry component to it. When the view with the UITextView in it is shown, I call [UITextView becomeFirstResponder] so the keyboard pops up. When the user taps done/save, the view controller calls pop on it's navigation controller, and the keyboard should disappear automatically.
This works fine on the iPhone/iPod touch, but on the iPad, the keyboard remains up, with the accessory view, even after the view is popped. I've tried everything: checking leaks with Instruments, static analyzer, explicitly calling [UITextView resignFirstResponder] multiple times, heap shots, and no matter what I can't get the keyboard to disappear once the view controller is popped or figure out why the heck it wouldn't be. This is a huge issue because the 'previous' view controller in the UINavigationController hierarchy doesn't have a text field, and the accessory view remains on top of the keyboard and when the buttons on it are tapped, they send messages to the deallocated view controller, causing a crash.
The only difference I can name between the iPhone/iPad version is that the iPhone version, of course, presents the navigation controller modally over the full screen, while on the iPad the navigation controller is presented in UIModalPresentationFormSheet.
I hope this is enough information to allow someone to diagnose the issue. If it's not, I'll post a sample project.
If I recall correctly, the keyboard always shows up while something is presented in UIModalPresentationFormSheet. Try something different and see if that works.
I am looking for a way to show my own input view (a UITableView) to enter certain keywords in a UITextView faster than typing them, and also be able to type into this text view the normal way. My solution has a button that causes the keyboard to disappear, revealing the table view underneath it.
Problem is I can't figure out how to make the keyboard go away without resigning first responder, and losing the cursor. Has anyone accomplished this before?
Thanks for any help.
Nope
As far as I know there is no way to do this, and I have searched extensively. Very frustrating that selection and the cursor are restricted to the keyboard.
I could be wrong though. Votes? Suggestions?
You can call becomeFirstResponder on some other thing that you choose. It could be a UIViewController or a UIView. I had a similar problem before, I needed to make my keyboard go away when I was pushing my view controller back to its caller, without knowing which textfield was the first responder. Then, on viewWillAppear of my view controller which I was returning back, I called [self becomeFirstResponder] and the keyboard of the pushed view was gone. Because this made whichever text field was it loose being the first responder.
The scope of this question is IPhone 3.1 sdk (app running in simulator still)
I have a table view that has a cell with a UITextField in that cell. The table view is grouped, and has one section with just a couple fields. Im NOT using IB so backgroundTap is out of the question (as far as i can tell at least). When i click the text field, keyboard shows. Hiding it is still troublesome. Ive pulled the UITextFieldDelegate into the mix to hide the keyboard but the textFieldShouldEndEditing method doesnt seem to fire when the background is tapped (when i mean background, im tapping outside of the grouped table view section). First off, should it?
textFieldShouldReturn fires with no problem and i can resign at this point but shouldnt i be able to resign if focus shifts away from that control?
Any help is much appreciated
-me
Generally you'll only stop editing a field when you:
hit the "Done" or action button on the keyboard
begin editing another field
exit the view
have another button on screen that removes focus
From any of these, you can call -[textField resignFirstResponder] to dismiss the keyboard and call your -textFieldShouldEndEditing: method. There's no reason that just tapping on a non-active part of the screen should dismiss the keyboard.