0

UIPickerView select and hide

I used this to pop up a date picker view when I "touch down" on a text field. That is, I need the text field to display whatever I choose on the date picker view as its contents.

 - (IBAction)showPicker:(id)sender {

    // create a UIPicker view as a custom keyboard view
    UIDatePicker* pickerView = [[UIDatePicker alloc] init];
    [pickerView sizeToFit];
    pickerView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    //pickerView.delegate = self;
    //pickerView.dataSource = self;
    //pickerView.showsSelectionIndicator = YES;
    self.datePickerView = pickerView;  //UIDatePicker



    fromDate.inputView = pickerView;



    // create a done view + done button, attach to it a doneClicked action, and place it in a toolbar as an accessory input view...
    // Prepare done button
    UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
    keyboardDoneButtonView.barStyle = UIBarStyleBlack;
    keyboardDoneButtonView.translucent = YES;
    keyboardDoneButtonView.tintColor = nil;
    [keyboardDoneButtonView sizeToFit];

    UIBarButtonItem* doneButton = [[[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                    style:UIBarButtonItemStyleBordered target:self
                                                                   action:@selector(pickerDoneClicked:)] autorelease];

    fromDate.text = (NSString *)datePickerView.date; // fromDate is the Text Field outlet, where I am trying to display the selection on the picker.

    [keyboardDoneButtonView setItems:[NSArray arrayWithObjects:doneButton, nil]];

    // Plug the keyboardDoneButtonView into the text field...
    fromDate.inputAccessoryView = keyboardDoneButtonView;  

    [pickerView release];
    [keyboardDoneButtonView release];
     }

    - (IBAction)pickerDoneClicked:(id)sender {

        [fromDate resignFirstResponder];
    }

Edit : I am able to do this for one text field on the screen. No matter what, only a keyboard pops up for the second text field.

Any ideas to do this for a second text field?

Community
  • 1
  • 1
esh
  • 2,842
  • 5
  • 23
  • 39

2 Answers2

0
fromDate.inputView = pickerView;

This line of code makes the picker view the input view for the fromDate field. You need similar code for the other date field. You'll also need to set the input accessory on your other field.

Creating a special method to do this and touch down actions on all your text fields is unnecessary work. Simply create the picker view and toolbar in viewDidLoad and assign them to the input views and accessory views of whatever fields you want them on. Text fields alread have a touch down method which will bring up the keyboard.

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • Also, I have checked the input accessory. – esh Jul 17 '12 at 10:55
  • I even tried setting another pickerView object and using it for the second text field. Not very smart, but I want to get this working. – esh Jul 17 '12 at 11:00
  • Your toDate value could be nil, then. Is the outlet connected? – jrturton Jul 17 '12 at 11:01
  • I think you didn't get what I was trying to say. I am unable to pop up a UIDatePicker for the second text field on the same view, like I am with the first field. – esh Jul 17 '12 at 11:04
  • Oh hang on, where is showPicker: called from? You should be setting the input views etc. on viewDidLoad, not from an action. – jrturton Jul 17 '12 at 11:04
  • It is called on a "Touch Down" event from the text field. I now have a showPicker: and a showToDatePicker: that is triggered from a second text field. – esh Jul 17 '12 at 11:08
  • The code in viewDidLoad is `datePickerView = [[UIDatePicker alloc] init]; [datePickerView sizeToFit]; datePickerView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); fromDate.inputView = datePickerView; toDate.inputView = datePickerView; ` It still doesn't pop up the UIDatePicker view for toDate. – esh Jul 17 '12 at 13:21
0

Just an alternate solution for your problem.

1) Put a custom UIButton over the textfield on tap of which you want to show the UIDatePickerView

2) set the delegate of the textfield to nil on which you want to show the picker view.

fromDate.delegate = nil;

3) on the IBAction("touch down") of that custom button write the below code :-

- (IBAction) customButtonAction: (id) sender
{
    [_previousTextField resignFirstResponder]; // The instance of last textfield tapped.

    [self showDatePicker];
}

4) On tap of "Done" button of Picker view set selected value to the textfield

- (IBAction)pickerDoneClicked:(id)sender 
   {
      fromDate.text = @"value from UIPickerView's selected row"
   }
Slim Shaddy
  • 138
  • 6