How To: hide keyboard when UITextView is out of view? - cocoa-touch

I've put some UITextViews in a UITableView(Controller) with custom cells, and I'm running into a problem. The user can hit the "Edit" button up in the navbar to set the table into editing mode - each custom cell has its own way of enabling its individual UITextViews for text entry. That works fine.
The problem I'm having is that if focus is on a text view that gets scrolled out of view when the user hits "Done", the keyboard remains and the textview remains in edit mode. This doesn't happen if the textview is in view. More specifically - the cell i'm looking at is at the top of the screen and will act funny if it's scrolled above the iPhone screen. I don't seem to have the same problem when the textview at the bottom of the table gets scrolled out.
I've done just about every single permutation I could think of to get the view to resign first responder, but it appears to me that the hidden textview doesn't get/send any messages (even any delegate methods) until it is back on to the screen OR some other text view gets focus.
What am I missing?

After working on this for the better part of a full day, this is what I learned:
You can't actually access
out-of-view cells in UITableView. I
guess that makes sense, though
frustrating in my situation
No amount of redrawing or trying to
manually resign first responder is
going to help, even when you point
to a specific cell in a specific
row.
setEditing: animated: (called when you hit the "Done" button) isn't the only thing going on
I'm going to guess that the reason the UITextView remains in edit mode is because the cached version of the cell is in edit mode and cached cells/data aren't called by these various table methods (like setEditing:animated:)
This is the solution I found: (at UISearchBar and resignFirstResponder):
* calling [self.tableView endEditing:YES] causes all views to resign first responder, which means my UITextView is no longer selected. I'm not sure why it works this way, but appears setEditing: is sent to each cell & redrawn accordingly.

Related

UITextField losing focus after button press and text field switch (keyboard remains visible)

I've got a UITableViewCell subclass here that manages a UITextField setup on the right side of the table view cell. Anywhere from 4 to 8 of these cells are displayed at any given time depending on the table; I use them for unit data entry (ie, entering in distances, temperatures, etc) so there's quite a bit of logic bolted to the cell subclass.
For whatever reason, I've noticed that if I perform the following steps:
1) Tap on a text field to begin editing and bring up the keyboard
2) Enter in some text
3) Tap the clear button (which is enabled on the text field)
4) Tap on another textfield in the same table view
Then the current UITextField loses focus, but the second text field does not gain it. This means that no UITextField currently has focus, but the keyboard is still being displayed on-screen... but without any active text field, it does nothing, and cannot be dismissed (presumably because there's no first responder to resign?).
I can then tap on another text field again, and it will take focus and begin editing- at which point the keyboard becomes operable again and pressing the return/done key will dismiss it and end editing as usual.
If I simply tap on another UITextField without first hitting a button, then the second UITextField will gain focus immediately (as I'd expect it to). But it seems like button presses outside of the UITextField will cause this behaviour to occur if you try to switch fields after tapping any kind of button other than the text field.
Does anyone know what is causing this? It almost sounds like there's something wrong with the responder chain, but I'm not sure what the problem would be or how to fix it.
Firstly,you are sure the textfiled in the table has a unique identifier ,such as tag.
Secondly,you should make another textfiled become first responder if you want a textfiled lost first responder but the keyboard still appear.
Figured out what it was...
The problem was that I was reloading the table data in the delegate method that my custom cell was calling upon edit completion. Apparently reloading the tableview data while you're in the middle of switching UITextFields will cause the second text field to not gain focus (but the keyboard won't get dismissed), hence causing the issue I was seeing.

Using tap gesture to dismiss keyboard, rest of screen no longer works

I have a UITableViewController that contains a UISearchBar in one of its cells. Following examples here, I put a addGestureRecognizer in my viewDidLoad to capture taps outside the searchBar and calls resignFirstResponder on the search bar so the keyboard is dismissed.
However, this seems to be trapping all taps, the other items in the tableView no longer respond.
This is odd, because I have the identical code (cut and pasted) in another screen, a UIViewController, and it works fine there. The user can continue clicking on other objects just fine.
Any ideas? I suspect this is a simple view hierarchy issue?
Ahhh, it seems the first version I wrote shouldn't work either. The key is to enable the tab gesture only when the searchBar is entered, and then disable it again when you exit. This question has all the code:
Cancel out of UISearchBar when user taps on view

How Can I Change UIImages in a ScrollView based on a User's Input

Okay gang, I'm scratching my head on how to accomplish this one so I wanted to put it out to the world at large.
Essentially what a client wants is a way to toggle PART of a view based on whether the user selects a "Yes" or "No" option. My question is how would I go about accomplishing that?
Allow me to provide some more details. Within this specific app resides a form (a form with custom text fields, picker views, switches, sliders and other UI elements) that have all been laid out in a Storyboard, then programmed to function. About 3/4 of the way down this form, the user will be greeted with a "Yes" or "No" option and a button for each. The trick is that each view needs to have its own UI elements (text fields, sliders and buttons) appear ONLY when one of the options is selected and only BELOW the "Yes" or "No" option (all the elements above it need to remain, stay in the same place and hold the information the user enters).
An example : If the user hits the "Yes" button, below it 3 lines of text followed by a UIButton and Text Field would appear. Underneath this, other navigation buttons which navigate to other ViewControllers would appear. However, if the user hits the "No" button , all of those items I mentioned a moment ago would need to disappear, and instead a different set of text fields, labels, buttons and background image would need to appear. The navigation buttons will also need to link to other ViewControllers, different from the buttons in the "Yes" option. At this point I suggested to the client that a Navigation controller rooted at the bottom of the screen would be a good idea, but they are vehemently opposed to this and instead want the navigation options to be "dynamic" (or change according to which option is pressed, "Yes" or "No").
I have thought to attempt this programatically by simply using each button to load in a different image, this works just fine. The hitch then becomes twofold;
1) When I attempt to load the other UI elements that I get no way to lay them out in a storyboard and thus they appear in sporadic locations and
2) All this needs to be contained within a scroll view, which ALSO needs to change its size depending on how much space is needed below the yes and no option. Naturally the "Yes" section is 3 times smaller than the "No" section.
So, any ideas on how to make all this happen? I should mention at this point that the client also does not wish to simply navigate to a different ViewController, they very much want all this to occur on the same screen. I wish I had some code to share but we are still in the "design" aspect of this project and as such, very little code has been written. Any advice will be much appreciated as I've never been greeted with this type of build request before.
Create a UINavigationController object and set your view(say yourViewController) where your Yes and No buttons exist as a root view controller as below
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:yourViewController];
Now when user press YES/NO button on yourViewController view than you can programmatically create all controls text field and buttons and set frame according to your view. like
UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(enter frame accordinglly)];
[self.view addSubview:txtField];
same you can add more controls like buttons or any other controls.
after that when user press any the button(on which you want another view to display) you can push other view in the navController as below
[navController pushViewController:otherViewController animated:YES];
you need to keep track of navController in your yourViewController
when you want to go back just pop view from navController
[navController popViewControllerAnimated:YES];
PS: please be watchful for memory deallocation as well.

How to suppress virtual Keyboard slide-in animation?

I've got a problem with creating a modal search view that emulates the behaviour of that of the Weather app. Specifically, there are two animations, that are bothering me and introduce unneeded 0.2 s delays:
When the modal view becomes visible, I give focus to the UISearchDisplayController.searchBar by caling becomeFirstResponder in viewDidAppear. However, the keyboard is not visible, when the modal view has slid in, but needs another 0.2s to slide in after the animation of tehe modal view transition is complete. Moving the call to another callback like viewWillAppear or viewDidLoad did no good, the keyboard won't show up in the first place.
When the user touches cancel, there is another animation taking place, before the delegate's searchDisplayControllerDidEndSearch method is called, expanding the search text field and "melting" away the button. Again, this animation is unneded as the modal view is supposed to transition out when the button is touched.
Additionally, when I dismiss and re-present the same view, not only does the keyboard slide in after the transition, but the cancel button does the same (luckily simultaneously).
I am aware of a similar problem described here: Keyboard Animation Issues When Calling becomeFirstResponder within a Modal View Controller.
However, it seems like the behaviour of the search bar is sligtly differet then that of text field. I could not reproduce the steps described by that author to make the keyboard visible by calling becomeFirstResponder in viewDidLoad.
Regards,
Chris
I think I found your answer. When you add a search bar using the interface builder, you can do it two ways: "Search bar" and "Search bar and Search Display Controller".
I was using the second and was having the very same problem you described. I could only invoke the keyboard (using becomeFirstResponder) on "viewDidAppear". But if you do it adding just the search bar it works. Now I can call becomeFirstResponder on "viewDidLoad" and the keyboard appears together with the view itself.
I means a little more work, but really not much. You have to set your controller to be the delegate of the search bar. I added a list view for the results and made my controller become its delegate and its datasource.

UISearchDisplayController not displaying keyboard when text area touched

I have a UITableView in a controller that is nested under a UITabBar.
The interaction is all wired up in Interface Builder so far, nothing done programmatically in terms of view switching.
I've added a UISearchDisplayController as the header of my UITableView. It displays fine, and when I tap on the text entry area, the cancel button appears and the black overlay flies in.
However, the keyboard never appears and when tapping the cancel button, the overlay flies out and the cancel button disappears, but the text entry area keeps focus and the caret stays flashing there, so I cannot tap there again to re-display the search results.
So essentially I have two problems:
Keyboard not appearing when starting to edit text on UISearchBar from UISearchDisplayController
UISearchBar not loosing focus when cancel button is tapped.
What am I doing wrong?
The .xib file that had my tab bar in it contained a UIWindow.
This lead to all sorts of craziness and in the end I gave up on trying to do this with interface builder, and resorted to constructing the UITabBar in code, thereby not creating a second UIWindow.
This resolved the problems and the UISearchDisplayController behaved correctly.
check this method in UISearchBarDelegate:
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar;
Try to see if this is getting called and do keyboard-related removal in here. If not, try making another UISearchDisplayController. (I actually never use the default viewController's one). Also, make sure the delegate is correctly set.