按钮单击时出现日期选择器

时间:2014-03-13 11:55:21

标签: objective-c ios7 datepicker

我是IOS开发的实习生。我有一个按钮,当我点击按钮日期选择器应该出现,在那个日期选择器我需要取消和完成栏按钮。当我点击完成日期应该出现在我的文本字段上(通过编程)。帮助我

-(void)calbtn:(id)sender{
    pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];

    picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
    picker.datePickerMode = UIDatePickerModeDate;
    picker.hidden = NO;
    picker.date = [NSDate date];
    [self.view addSubview:picker];


    UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    pickerToolbar.barStyle = UIBarStyleBlackOpaque;
    [pickerToolbar sizeToFit];
    NSMutableArray *barItems = [[NSMutableArray alloc] init];


    doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];
    [doneBtn setBackgroundVerticalPositionAdjustment:50.0f forBarMetrics:UIBarMetricsDefault];
        [doneBtn setBackButtonBackgroundVerticalPositionAdjustment:50 forBarMetrics:UIBarMetricsDefault];

     [barItems addObject:doneBtn];
    UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)];
    [barItems addObject:cancelBtn];
    [pickerToolbar setItems:barItems animated:YES];

    [pickerViewPopup addSubview:pickerToolbar];
    [pickerViewPopup addSubview:picker];
    [pickerViewPopup showInView:self.view];
    [pickerViewPopup setBounds:CGRectMake(0,0,320, 464)];

如何设置完成框和取消栏按钮。

2 个答案:

答案 0 :(得分:0)

只需使用内部带有日期选择器的UIActionSheet,并使用您想要的按钮在其顶部显示。

答案 1 :(得分:0)

使用UIToolbar创建AccessoryView,其中UIBarButtonItems为Done和Cancel。并为文本字段添加输入访问视图

     [cellTextFeild setInputAccessoryView:fieldAccessoryView];

然后选择pickerview将其添加到视图中并将其添加为textfield的输入视图。

     [cellTextFeild setInputView:pickerBGView];