tags:

views:

2316

answers:

3

i want to show a pickerview when i click on a uitextfield not the keyboard and fill the value in text field form the picker view..

any one knows this???

A: 

Check out the code at (there is both textview+tabbar and pickerview+tabbar code)

http://stackoverflow.com/questions/885002/uitextview-and-uipickerview-with-its-own-uitoolbar/885342#885342

this will get the pickerview to resign etc. All u need to do then is use the pickerview delegate method to update the text view

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

Instead of using a textfield, use a UIlabel, with a white background. That way the keyboard will not show when you tap it. override the touches event on the UILabel, when that happens call the method form the privious question that will display the new view.

 -(void) addToViewWithAnimation:(UIView *) theView
Bluephlame
i do not want the keyboard to show up only picker view is i want to be displayed when i enters in text field
Rahul Vyas
yeah the keyboard will pretty much always show up. you will have to use another view such as uiLabel. i've edited the answer to be more concise.
Bluephlame
As Blue mentioned, the keyboard will always show up and you can't avoid it. Another alternative would be to add a picker view on top of the keyboard. They are the same height, so the keyboard won't be seen.
lostInTransit
+6  A: 

It is possible to prevent the keyboard from popping up. Set your class to be the delegate of the UITextField: textField.delegate = self and add: <UITextFieldDelegate> after your interface declaration (before the {) in the header file.

Now implement textFieldShouldBeginEditing::

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    // Show UIPickerView

    return NO;
}

If you want to be able to hide your pickerView this method won't work. What you could do then is create subclass of UITextField and override the *trackingWithTouch:event: selectors and do your magic in those. You probably still need to return NO from textFieldShouldBeginEditting: to prevent the keyboard from showing.

klaaspieter
your solution works partially.i want to show picker view on some specific uitextfields not on all text fields and also when the value is selected from the picker view filled in the desired text field the picker view must be hidden.
Rahul Vyas
You can show it on specific textfields by comparing the textField variable from the textFieldShouldBeginEditting: selector to (for example) an instance variable: if (textField == _pickerTextField) { /* Do your thing */ }As for hiding the picker, when exactly do you want to hide it? How do you determine when the user is done picking. Keep in mind that a user can 'mis-pick' so you should not assume that the user is done when valueDidChange is fired.
klaaspieter
A: 

Hey I have pasted in some code I wrote a while back to cover this scenario. There are two examples one with an actionsheet and one without an actionsheet:

- (void)textFieldDidBeginEditing:(UITextField *)myTextField{

             [myTextField resignFirstResponder];

             actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];

             [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

             CGRect pickerFrame = CGRectMake(0, 40, 0, 0);

             UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];

              pickerView.showsSelectionIndicator = YES;

              pickerView.dataSource = self;

              pickerView.delegate = self;

              [actionSheet addSubview:pickerView];

              [pickerView release]; //NB this may result on the pickerview going black the next time you come back to the textfield, if this is the case just remove this statement

              UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:NSLocalizedString(@"SUBMIT", @"")]];

              closeButton.momentary = YES;

               closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);

               closeButton.segmentedControlStyle = UISegmentedControlStyleBar;

               closeButton.tintColor = [UIColor blackColor];

               [closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];

               [actionSheet addSubview:closeButton];

               [closeButton release];

               [actionSheet showInView:displayedInView];

               [actionSheet setBounds:CGRectMake(0, 0, 320, 485)];

}

This is the code without the actionsheet:

- (void)textFieldDidBeginEditing:(UITextField *)myTextField{

         [myTextField resignFirstResponder];

          for (int component = 0;  component &lt; (((NSInteger)numberOfComponentsForPickerView) - 1); component++) {

              NSInteger valueForRow = [[self.textField.text substringWithRange:NSMakeRange(component,1)] integerValue];

               [pickerView selectRow:valueForRow inComponent:component animated:YES];

          }

           [self fadeInPickerView];

            [view addSubview:pickerView];

            [pickerView release];

}
wibo