I know its very basic question, but i'm facing a strange behaviour with UIPickerView.
Here is my scanario - I'm using UIPickerView in my app. my problem is that when i click on a row didSelectRow method is not called, however when i scroll rows of picker then its working.
more specific assume that first row on picker is currently selected and if i click on 4th row then didSelectRow method not fired.
What am i missing?
UPDATE: if i comment this code from viewDidLoad method then all works fine-
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
and
-(void)dismissKeyboard {
[numberTextField resignFirstResponder];
[nameTextField resignFirstResponder];
[cityTextField resignFirstResponder];
[addressTextField resignFirstResponder];
[zipTextField resignFirstResponder];
}
I'm assuming you're trying to use the tap gesture recognizer to dismiss the keyboard if they click anywhere in the view. The problem this is causing is now your UIPicker is not getting touch events passed to it. I have two ideas for possible solutions.
1) Inside the method:
(CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(UIView *)view
test the location and/or the view to determine if the picker was touched or not, and then forward the event.
2) Instead of adding the tap recognizer to the entire view, add an invisible subview to the likely tap area to close the keyboard that won't overlap the picker.
Did you try adding Gesture recognizer to other parts of self.view than the pickerview?
Related
I added a PickerView to an TextField, now I want to allow the user to tab on the selected row and then the item must show in the textfield und the pickerView must hide.
I try this but I does not work.
Any help here?
- (void)textFieldDidBeginEditing:(UITextField *)textField {
UIPickerView *pickerViewGeschlecht = [[UIPickerView alloc] init];
pickerViewGeschlecht.dataSource = self;
pickerViewGeschlecht.delegate = self;
UITapGestureRecognizer *myGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(pickerTapped:)];
[pickerViewGeschlecht addGestureRecognizer:myGR];
self.txtGeschlecht.inputView = pickerViewGeschlecht;
}
- (void) pickerTapped:(UITapGestureRecognizer*)gestureRecognizer{
txtGeschlecht.text = ausgewaehltesGeschlecht;
}
I am having the same problem. It works great in ios 6.x but it seems like the functional changed in ios 7.x. Now tapping seems to default to picking the row you tapped, and you can no longer set your own tap gesture. There must me a simple Boolean value I have to set, I just can't find it anywhere.
Allowing user to select a UIPickerView row by tapping
This might be how you have to handle the tap now, I like the solution of putting a clear uicontrol over the picker and attaching the tap action to the puckers delegate.
By default when tapping on a UITextField iOS will display a default keyboard. Is it possible to bypass this? I would like to display modally a custom view controller on tap on the textField and be able to edit the textField through this controller.
Is there a recommended way?
Following wil repalce the keyboard as the input view when the user clicks on the UItextField.
self.TextField.inputView = "your view ";
Ok tried out the exact requirement you asked for:-
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[textField resignFirstResponder];
POCModalViewController *objPOCModalViewController = [[POCModalViewController alloc]init];
[self presentViewController:objPOCModalViewController animated:YES completion:nil];
return NO;
}
Where POCModalViewController is the controller you want to present.
I would like to post the solution i have finally implemented, which is the closest to Footyapps27 solution:
I have made the controller that will present the modal controller(which will contain internally multiple custom keyboard views) as the uitextfield delegate for any UITextField objects contained within the view of my controller.
I can now received any notification through the - (BOOL)textFieldShouldBeginEditing:(TWValueInput *)textField method when a textfield start to be edited:
Within that delegate method I have the following code snippet:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
MyCustomKeyboardVC* vc = [[UIStoryboard storyboardWithName:#"main" bundle:nil] instantiateViewControllerWithIdentifier:#"customKeyboardController"];
vc.delegate = self;
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:vc animated:NO completion:nil];
return NO;
}
returning NO within that method will prevent the default keyboard from being displayed. There is no need actually to call the resignFirstResponderon the textfield.
I should point out though that the Apple recommended way to display a custom keyboard is to provide a custom view to the textfield inputView property like Divya mentioned. Since i wanted to managed multiple keyboard view entries it was quicker for me to display a custom keyboard controller through the delegate method i mentioned above.
So Ive added a TapGestureRecognizer to the navigation bar in order to "pull down" another view, thus its a UIPanGesture Recognizer. The problem is when this gesture is added the UITableView scrollToTop method no longer works, even if enabled before or after the addition of the gesture recognizer.
Has anyone ever experienced or can think of an easy solution?
Thanks!
Heres My Code:
if (!pan) {
_pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panaction:)];
[_pan setMaximumNumberOfTouches:1];
}
_pan.delegate = self;
[self.navigationController.navigationBar addGestureRecognizer:_pan];
EDIT:
Ok, the problem seems to be adding a UIView as a subview in order to drag down from the UINavigation bar, when I dont add the subview the scrollToTop works fine, once its added it must be intercepting the touch event underneath the status bar..
I have a UITextField in my iOS app. When a user enters text and clicks Return, the keyboard goes away due to a call to an IBAction with "resignFirstResponder."
However, XCode does not let me drag a line from the UIView itself to File Owner. How do I associate touching the background of a UIView with an IBAction that makes the keyboard go away?
You can use UITapGestureRecognizer. see: Dismiss keyboard by touching background of UITableView
so instead of tableview, just add it to your view instead:
UITapGestureRecognizer *gestureRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideKeyboard)] autorelease];
gestureRecognizer.cancelsTouchesInView = NO; //so that action such as clear text field button can be pressed
[self.view addGestureRecognizer:gestureRecognizer];
and have a method to hide your keyboard
- (void) hideKeyboard {
[self.view endEditing:YES];
}
You've already noticed that you can't drag from the UIView to the file's owner to assoctiate an action with a touch.
The way to work around this is to change the class of the background view from UIView to UIControl and hook up an action from there to a method in your controller to stop editing.
That's because a UIControl can respond to touch events, and a UIView does not, but a UIControl subclasses UIView, and so it can be used in place of a UIView.
I wrote an example project a while ago that uses this technique. Have a look at the secondViewController's xib file and see how I've change the class of the background view and hooked it up to a an action in the controller to dismiss the keyboard.
Use the touchesBegan with Event and end editing on the view:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
One easy way to do it is to create a big transparent UIButton behind the view.
When an UITextField is firstResponder, I would like to bring to front an UIDatePicker (bottom part of the screen) without the "going down keyboard" (no call to UITextField resignFirstResponder).
The aim is to process like UIKeyboard of UITextField which pop-up on nearly everything when it becomeFirstResponder. modalViewController seems to be fullscreen only.
- showDatePicker:(id)sender {
if([taskName isFirstResponder]) [taskName resignFirstResponder];
[self.view.window addSubview: self.pickerView];
// size up the picker view and compute the start/end frame origin
(...)
[UIView commitAnimations];
}
This example is an animation of keyboard going down, and DatePicker going up, behind and not in front.
Do you know a solution ? A piece of code would be welcome. Thanks in advance.
This is simply done by setting the input view of the text field to the Picker View. Then, on Editing did begin tell the picker view to becomeFirst responder. Like this
textField.inputView = pickerView
then using an IBAction called when the Editing Did Begin
-(IBAction) setPickerViewAsFirstResponder:(id)sender
{
[pickerView becomeFirstResponder];
}
This works perfectly. You'll need to implement code to actually set what the picker view is currently showing to be a string in the text field still.
This definitely can be done... simply implement the method below after setting UIViewController <UITextFieldDelegate> in your .h
Long story short, this overrides the keyboard loading before text editing begins.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
// Make a new view, or do what you want here
UIDatePicker *pv = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,245,0,0)];
[self.view addSubview:pv];
return NO;
}