如何在uidatepicker上添加完成按钮

时间:2014-09-19 12:52:22

标签: ios

 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)];

当我按下文本字段时,如何在uidatepicker顶部添加完成按钮,然后uidatepicker将在输入完成日期后显示按钮也显示。单击完成按钮后,uidatepicker应隐藏

3 个答案:

答案 0 :(得分:0)

enter image description here分配视图,将datepicker底部的datepicker添加到视图的datepicker顶部的视图和工具栏上。 将条形图添加到工具栏中。

答案 1 :(得分:0)

//首先声明所有属性并在.h中设置委托和数据源,如此

@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;   

//和.m文件中的综合

@synthesize picker,actionPicker,toolbarPicker;

//在View中执行load方法put调用picker的设置方法

 [self pickerSetting];

//通过此方法设置选择器,操作表和工具栏

 -(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];
}

//添加两个mehtod for picker done按钮并取消按钮点击。

-(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];

}

//并放置委托和数据源方法

#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];
}

//为分配选择器调用此方法。

[self pickerSetting] 

//并使用此显示选择器(将此按钮设置为按钮操作方法)

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

我希望,这对你有帮助。谢谢。

答案 2 :(得分:0)

我认为您可能真的想使用inputAccessoryView的{​​{1}}属性。只需在UITextField放置一个UIToolbar,然后使用目标操作调用UIBarButtonItem