29

I've read a lot about how I can use a UIPickerView as the inputView of my UITextField. The thing is, I am able to call the UIPickerView when I tap on the UITextField. However, my app always loads with the UIPickerView shown. I have already tried changing the myownpickerview.hidden = YES; in viewDidLoad, but this causes problems when I click on the UITextField. It won't show up, and if I click multiple times, the debugger shows that there would be an error.

Can anyone point me in the right direction? I only want the UIPickerView shown after I tap on the UITextField

I'm still working on my first ever iOS app. Please be patient. Thank you =)

iPatel
  • 46,010
  • 16
  • 115
  • 137
SilverHood
  • 713
  • 1
  • 8
  • 15

3 Answers3

51

Try this, it works fine, put it in viewdidload.

yourpicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 50, 100, 150)];
    [yourpicker setDataSource: self];
    [yourpicker setDelegate: self];
    yourpicker.showsSelectionIndicator = YES;
    self.yourtextfield.inputView = yourpicker;

do not [self.view addSubview: yourpicker]; this

codercat
  • 22,873
  • 9
  • 61
  • 85
Aklesh Rathaur
  • 1,837
  • 18
  • 19
  • I'm not sure what to do with the frame. Am I supposed to position it out of the screen? If so, will it come back when I click on the `UITextField`, and how to do it? Thanks! – SilverHood Dec 23 '13 at 10:18
  • SilverHood, it shouldn't show until the user taps on the UITextField. Perhaps you are allocating another UIPickerView somewhere else? The code should just use this one. – auspicious99 Dec 23 '13 at 10:30
  • i have edited my answer and wrote a note check that or dont add pickerview as subview . – Aklesh Rathaur Dec 23 '13 at 10:32
  • @auspicious99 does it matter that I created the `UIPickerView` using the storyboard? I dragged and dropped it there. Right now, if I don't add `self.` in front of my `UIPickerView` name, I get errors – SilverHood Dec 23 '13 at 10:37
  • @AkleshRathaur I've accepted your answer. I copied your code exactly, just that I added `self.` in front of my `UIPickerView`'s name. Doing so actually leaves 2 lines on the screen (I presume it's the selection indicator), but no matter how I do it, it doesn't go away. The only way is to drag the `UIPickerView` on the Storyboard all the way down. Any solutions for this? Other than that, thank you very much! – SilverHood Dec 23 '13 at 10:55
  • 3
    @SilverHood you should create the `UIPickerView` programatically, as a property inside your view controller. For example, `@property (strong, nonatomic) UIPickerView *yourPicker;`. Also, there is no need to set the frame, you can do it like this `self.yourPicker = [[UIPickerView alloc] init];` – Scott Jun 25 '14 at 10:33
  • Thanks for the tip, that helped me too, but How can i make the UIPickerView disappear after the user selects a value in it? – Marlon Jul 10 '15 at 14:28
  • @Marlon please put "[self.view endediting:yes]" in didselect row of pickerview. – Aklesh Rathaur Jul 13 '15 at 05:06
1

Use like this,

 // Picker
UIPickerView *picker = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,150)];
picker.dataSource = self;
picker.delegate = self;

// Tool bar
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,35)];
pickerToolbar.barStyle = UIBarStyleDefault;

// Bar button
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDoneButtonTapped:)];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[pickerToolbar setItems:@[flexSpace,doneButton]];

self.selectedLockerTextFiled.inputAccessoryView = pickerToolbar;
self.selectedLockerTextFiled.inputView = picker;

Below code for done button actions,

-(void)pickerDoneButtonTapped:(id)picker{
    [self.view endEditing:YES];}
0

Using Swift:

let pickerView = UIPickerView()
pickerView.dataSource = self
pickerView.delegate = self
textField.inputView = pickerView;

Don't forget to implement the UIPickerViewDataSource and UIPickerViewDelegate.

pableiros
  • 14,932
  • 12
  • 99
  • 105