2
UIDatePicker *datePicker = [[UIDatePicker alloc]init];
[datePicker setDate:[NSDate date]];
[datePicker addTarget:self action:@selector(updateTextField:) forControlEvents:UIControlEventValueChanged];
[txt_time setInputView:datePicker];

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

How to add done button on top of the uidatepicker when I press on textfield then uidatepicker will show after entering the date done button also show. After clicking on done button uidatepicker should be hidden.

leonheess
  • 16,068
  • 14
  • 77
  • 112
Pintu Rajput
  • 621
  • 2
  • 8
  • 23
  • Of all the answer on StackOverflow for how to add a done button to a date picker, this one: https://stackoverflow.com/questions/12096118/display-uidatepicker-in-uiactionsheet-when-tapping-on-uitextfield was the most helpful. It achieves exactly the desired result with minimum code and custom configuration. – borisz Nov 19 '14 at 17:47

3 Answers3

0

enter image description hereAlloc a view add datepicker at bottom to the view and toolbar on the top of datepicker to the view. Add barbuttons to the toolbar.

Ramesh Muthe
  • 811
  • 7
  • 15
0

//First declare all the property and set the delegate and datasource in .h like this

@interface RegistrationView : UIViewController <UIPickerViewDataSource,UIPickerViewDelegate,UIActionSheetDelegate>
{
    UIPickerView *picker;
    UIToolbar *toolbarPicker;
    UIActionSheet *actionPicker;
}
@property (strong,nonatomic)  UIPickerView *picker;
@property (strong,nonatomic)  UIToolbar *toolbarPicker;
@property (strong,nonatomic)  UIActionSheet *actionPicker;   

//and synthesis in .m file

@synthesize picker,actionPicker,toolbarPicker;

// in View did load method put call the setting method for picker

 [self pickerSetting];

// set picker,action sheet and toolbar through This method

 -(void) pickerSetting 
{

    toolbarPicker = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,self.view.bounds.size.width,44)];
    if (!g_IS_IOS_6)
    {
      toolbarPicker.barTintColor = [UIColor colorWithRed:72.0/255.0 green:197.0/255.0 blue:87.0/255.0 alpha:1.0];
    }
    toolbarPicker.frame=CGRectMake(0,0,320,44);
    actionPicker = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil  destructiveButtonTitle:nil otherButtonTitles:nil];
    actionPicker.frame = CGRectMake(0, 234, 320, 256);
    [actionPicker setActionSheetStyle:UIActionSheetStyleDefault];
    actionPicker.delegate = self;

    UIImage *imgbtnConvert = [UIImage imageNamed:@"info_button.png"];//Done btn.png
    UIButton *btnConvert = [UIButton buttonWithType:UIButtonTypeCustom];
    btnConvert.bounds = CGRectMake(200, 0, 93,31);
    [btnConvert setTitle:@"Done" forState:UIControlStateNormal];
    [btnConvert setTintColor:[UIColor whiteColor]];
    [btnConvert setBackgroundImage:imgbtnConvert forState:UIControlStateNormal];
    [btnConvert addTarget:self action:@selector(pickerDoneClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barButtonItemConvert = [[UIBarButtonItem alloc] initWithCustomView:btnConvert];

    UIBarButtonItem *flexConvert = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

    UIImage *imageConvert = [UIImage imageNamed:@"info_button.png"];//cancel png
    UIButton *buttonConvert = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonConvert.bounds = CGRectMake( 0, 0, 93,31);
    [buttonConvert setTitle:@"Cancel" forState:UIControlStateNormal];  //set title
    [buttonConvert setTintColor:[UIColor whiteColor]]; //font color
    [buttonConvert setBackgroundImage:imageConvert forState:UIControlStateNormal]; //set background image
    [buttonConvert addTarget:self action:@selector(pickerCancelClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barCancelButtonItemConvert = [[UIBarButtonItem alloc] initWithCustomView:buttonConvert];

    NSArray *itemsConvert = [[NSArray alloc] initWithObjects:barCancelButtonItemConvert, flexConvert, barButtonItemConvert, nil];
    [toolbarPicker setItems:itemsConvert];
    [actionPicker addSubview:toolbarPicker];

    picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 320, 216)];
    picker.delegate = self;
    picker.dataSource = self;
    picker.showsSelectionIndicator = YES;
    picker.backgroundColor= [UIColor whiteColor];
    [actionPicker addSubview:picker];
}

// and add two mehtod for picker done button and cancel button click .

-(void) pickerDoneClick
{
    // picker done button click put here your code ..
    [actionPickerSearchCar dismissWithClickedButtonIndex:0 animated:YES];

}

-(void) pickerCancelClick {
    // Cancel button click 
    [actionPickerSearchCar dismissWithClickedButtonIndex:0 animated:YES];

}

// and put the delegate and data source method

#pragma mark -
#pragma mark UIPickerView Delegate Datasource Method

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {

   // return yourarr.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{

   // return yourarr[row];
}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
   // selectedfiles =yourarr[row];
}

// call this method for allocation picker .

[self pickerSetting] 

// and show picker using this (put This in button action method)

-(IBAction)showPicker:(id)sender
{
    [actionPickerSearchCar showInView:self.view];
    [actionPickerSearchCar setBounds:CGRectMake(0, 0, 320, 465)];
}

I hope , it's helpful for you . Thank you.

Ilesh P
  • 3,940
  • 1
  • 24
  • 49
0

I think you might really want to use the inputAccessoryView property of UITextField. Just put an UIToolbar there with your UIBarButtonItem and use the target action to call [txt_time resignFirstResponder].

DeFrenZ
  • 2,172
  • 1
  • 20
  • 19