ios Hiding the Keyboard - iphone

My Question is : What is the best way to implement "Hiding Keyboard while PickerView (which is inside the PopOver) is Visible"?
What I Want :
I have one view which has around 15 TextFields. Out of 15 , say 7 uses UIPickerView ( that resides into UIPopOverController ) to implement the DropDown Functionality. My Problem is when the KeyBoard is Visible and I Click on the UITextField with UIPickerView, KeyBoard is not hiding. What is the best way to implement this ?
What I Tried :
I tried to implement inputView but it is only used for UIPickerView while I have UIPickerView inside UIPopOverController. I think that's why I am unable to use inputView property. According to this Question, it is Bug from Apple.
Update :
Look , I know that using textField Delegates , we can implement this. But I found inputView property of UITextField and I found it quite interesting as you have to just assign it as :
textField.inputView = pickerView;
But i have the UIPickerView inside PopOver. So How to use this inputView property of UITextField ?
Any Better Idea ?...

I guess you can not do this directly (as inputView). You can try this simple work around:
1) Replace your UITextField with UIButton and connect it to some selector (buttonClicked:). You can use UIButtonTypeCustom to be able to style it so it looks like text field (set layer's bordeColor, borderWidth, cornerRadius, etc..)
2) In this method -(void)buttonClicked:(id)sender - toggle the same UIPickerView you would use as inputView for replaced UITextField. You can make in ordinary instance variable, just remember to set delegate and dataSource. Attach it to the root view (of your UIViewController show in popover) or even UIWindow and animate it in or out (toggle) depending if it is already shown or not.
3) Update button's text (by using [button setTitle:<selected_value> forState:UIControlStateNormal]) in UIPickerViewDelegate method: `pickerView:didSelect...atIndex: - you can additionally hide picker here..
Hope this would help.

Take an object in your .h file as:
id currentTextField;
Implement this UITextField Delegate Method in your .m file:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
currentTextField = textField;
}
Don't forget to make the txtField.delegate = self for all the UITextFields in your file.
Now when you receive the tap on your pickerView Button, first add a statement on that button's click method:
[currentTextField resignFirstResponder];
All the best!!!

Related

Strange Behavior-Did select row touch not responding for UITableViewCell

I have a very strange problem,I don't know whether it is awkward to normal behavior of cells or not,it seems as if it is so!Hence I am giving it up to some one who can answer, apologize if any thing stupid in asking this question.Normally,when we touch a table view cell,what happens is it navigates to a view controller/a controller that is coded.Now what's strange here is it is not responding to selection,or touch.I have checked whether or not allows selection while editing is selected in IB or not.I have selected it,now the twist here is when I am touching a table view cell it is not responding,instead when I swipe it horizontally/when I long press the cell,it is navigating,really surprised at this strange behavior!I don't understand the reason why I need to swipe it to make selection of cell in table view work.This is also happening with button present below the table view!
I have searched for issues similar to my case,but I found just one question there it was suggested to check for this method,
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
But i haven't implemented that method at all,what I have actually is a tab bar with several items,where I have a controller called AddController,for accessing several attributes and strings etc.. declared in the controller,I am subclassing it as follows:
#interface ViewController : AddController
Now because it was specified in the question I saw,i.e. the link I gave,to check whether u are copying the same code in subclass controller page,I spoke about subclassing and what I did,hope every one understands it!
Can any one please guide me how to get out of this issue,and make table view cell respond to normal touches,any help is greatly appreciated!
Thanks all in advance :)
After doing some research I'm pretty sure that it is the UITapGestureRecognizer on the tableView caused you the problem. If you were like me having the text field in the cell and using the UITapGestureRecognizer to close the keyboard, here's my solution:
In the view that you implemented UITextFieldDelegate
(In my case I have a custom UITableViewCell called TextFieldCell),
Declare a UITapGestureRecognizer as a property:
#interface TextFieldCell : UITableViewCell <UITextFieldDelegate>
{
UITextField *theTextField;
UITapGestureRecognizer *gestureRecognizer;
}
#property (nonatomic,retain) UITextField *theTextField;
#property (nonatomic,retain) UITapGestureRecognizer *gestureRecognizer;
And initialize it in your view:
self.gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(closeKeyboard:)];
In the - (void)textFieldDidBeginEditing:(UITextField *)textField method, use superView to move up to your tableView and call addGestureRecognizer:
[self.superview.superview addGestureRecognizer:gestureRecognizer];
And in the - (void)textFieldDidEndEditing:(UITextField *)textField, just remove the gesture recognizer:
[self.superview.superview removeGestureRecognizer:gestureRecognizer];
This will totally solve the problem.
Did you use a UITapGestureRecognizer on your tableView? I had the exactly same problem as you did today, and then I figured out that it was the gesture recognizer that blurs my intention for selecting a row by tapping. When I removed it, the tableView was back to normal.
Hope this helps.
I had this same problem and solved it by ticking "Scrolling Enabled" in the table view attributes.
My table view doesn't need scrolling, so it doesn't affect the app in any other way, except now I don't get the first unresponsive tap after a swipe gesture.

Unable to Generate UITextField dynamically in place of UILabel?

I am trying to generate UITextField dynamically in place of UILabel.
Now i want to update that data.
I am displaying data in the UILabel from the database and there is an UIButton for editing.
When i click on that UIButton UITextField should be generated in place of UILabel and also data should be displayed in UITextField.
What you can do is to design a view with all textfield which works in two modes, first readonly (by setting userInteraction to false ) and second editing mode. This way you can avoid the use of labels. This will need only one edit button for all of the fields. if you still want to stick with your approach, you can hide the labels, use their frames to create textfields at their place and make them visible as long as you are working in edit mode. Don't forget to use
[self.view bringSubviewToFront:TEXT_FIELD];
While you add them to your view.
e
Managing the editing with the approach I mentioned earlier is mor easy and require less efforts. Hope it helps
you need to implement this textField in .h file, to get access to it when you finish aditing. Then, in your button callback:
textField = [[UITextField alloc] initWithFrame:[yourUILabel frame]];
[textField setText:yourUILabel.text];
[self.view addSubView:textField];
then, to replace it back:
[yourUILabel setText:textField.text];
[textField removeFromSuperView];
You can use the methods that others have described here but it sounds like you just want a UITextField that looks like a label and you want to control whether or not it's editable.
set enabled to YES / NO depending on whether you want the user to edit the UITextField
set the borderStyle of a UITextField (usually between UITextBorderStyleRoundedRect and UITextBorderStyleNone)
You can then toggle the enabled and borderStyle values as follows:
- on initial view: enabled: NO, border style: UITextBorderStyleNone
- on button tap: enabled: YES, border style: UITextBorderStyleRoundedRect
You don't have to mess with the view hierarchy and worry about frames, etc, this way.

show subview when UITextField touched

I'm pretty new to iPhone development, so please excuse my ignorance. I've googled this a bit and so far come up with nothing useful. Here is what I would like to do:
I would like a subview to popup(with the rest of the screen showing in the background) when a UITextField is touched. The popup is a calculator UIView that I created in the IB. It seems it is better to have a popup show than a customized keyboard, due to Apple's developer guidelines.
Here is my question. How do I capture the touch on the UITextField and how do I show the subview?
I have tried things like below to show the subview, with no luck:
CustomCalculator *customCalc = [[CustomCalculator alloc] initWithNibName:#"CustomCalculator" bundle:nil];
UIViewController *calcController = [self.customCalc.view];
[self.view addSubview:calcController.view];
Use the delegate method:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
//Add your subview here
return NO; //this will stop the keyboard from poping up
}
This way when someone taps the textfield, your view will popup instead of the keyboard.
Now once the user is interacting with your view, you will have to manipulate the string in the textfield.text property directly as a reaction to the User tapping buttons in your view.
Implement the UITextFieldDelegate and the method for it is
-(void) textFieldDidBeginEditing:(UITextField *)textField
The above method is fired when you touch the UITextField. You may then position the UIPopoverController (I'm guessing that is what you're using to show the view in a popup) and as soon as you're done there pass the values back to the UITextField. Hence the popover's/viewcontroller presented's delegate should be your textfield object.
EDIT: After seeing the other answer below it struck me that I forgot to tell you how to stop the keyboard from showing. Just make this the first line in the method I've mentioned above:
[textField resignFirstResponder];

Multiple UIText fields with Multiple UIPickerViews in UITableView

I have a grouped style UITableView with a HeaderView that is loaded from another .xib.
The HeaderView has 4 UITextFields in it:
2 of the fields should display the Keyboard and allow user input.
2 of the fields should display a UIPickerView and update the UITextField with selection.
The Main TableView (with the sections and rows) is filled with UITextFields as well.
The UITextField in the first section (indexpath.section = 0) displays a UIPickerView
All other UITextFields in the rest of the sections/rows should display the Keyboard
I can get the Keyboard to display correctly and dismiss when the Done button is touched for all the UITextFields that can display the keyboard.
I can get the UIPickerView to display correctly and dismiss (with Custom Save/Cancel buttons).
The problem I have is when mixing the two...
When I do the following I have a hybrid effect:
Step 1: Touch the first UITextField to begin to enter data with the Keybard.
Step 2: Enter some data in the UITextField.
Step 3: Touch a UITextField that displays a UIPickerView instead of the Keyboard.
The result is the Keyboard AND my custom UIPickerView being displayed at the same time with the Keyboard actually displaying ON TOP of my UIPicker!
Any suggestions on how to prevent this from happening?
Need to show some code, but most likely you're not calling [myTextField resignFirstResponder] just before calling the UIPickerView.
You need to resign your keyboard for your textField. From the fact that you did not do it, I am guessing that you did not resign the pickerView as well. So you need to resign both of them or they will continue to stay on the screen
Resigning the textField.
Implement UITextFieldDelegate
Implement
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return 1;
}
Resigning pickerview:
Implement the picker view as your property and implement [self.yourpickerview resignFirstResponder] in textFieldDidEndEditing and in the IBAction of your custom DONE button for your picker view.

How to hide the iPhone keyboard? resignFirstResponder does not work

I have the following code...
- (void)textFieldDidBeginEditing:(UITextField *)textField {
//some code here...
NSInteger theTag = textField.tag; //I set the tag to 5 in IB
if (theTag == 5) {
//self.showDatePicker;
[textField resignFirstResponder];
}
}
The problem is, the keyboard never disappears. Another thing to note is that I have some other methods that move the view up and down based on the position of the textfield selected. Maybe that's messing up my Responder, but I just don't understand why the keyboard won't go away.
Also, I might just be doing this all wrong. I want this textField, when pressed, to hide the keyboard and show a date picker. Should this be in a different method?
If you're targeting iOS 3.2+, I'd suggest looking into UITextField's inputView property. You can assign a custom view (i.e. a data picker) to be displayed when the text field becomes the first responder instead of the keyboard.