向UIDatePicker添加按钮

时间:2010-09-02 05:18:24

标签: iphone xcode uikit

是否可以在UIDatePickerView中添加“完成”按钮,以便用户可以点按它来关闭视图。

4 个答案:

答案 0 :(得分:8)

您应该创建一个UIView来保存选择器和所需的按钮,然后在需要输入日期时将该视图设置为可视屏幕,并在点击按钮时将其设置为动画。我使用了这样的方案,其中按钮实际上是日期标签,并被点击以开始或结束日期编辑。

答案 1 :(得分:8)

我发布的代码请按照其类型声明未声明的对象休息你会完成它。希望这有帮助......

#pragma mark DatePickerView
UIImagePickerController* imagePickerController;
UIDatePicker *theDatePicker;
UIToolbar* pickerToolbar;
UIActionSheet* pickerViewDate;
-(void)DatePickerView
{
    pickerViewDate = [[UIActionSheet alloc] initWithTitle:@"How many?"
                                                 delegate:self
                                        cancelButtonTitle:nil
                                   destructiveButtonTitle:nil
                                        otherButtonTitles:nil];

    theDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
    theDatePicker.datePickerMode = UIDatePickerModeDate;

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
    [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]autorelease]];
    [dateFormatter setDateFormat:@"dd MMM yyyy"];
    //[dateFormatter setDateFormat:@"MM/dd/YYYY"];

    //[theDatePicker release];
    [theDatePicker addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged];


    pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    pickerToolbar.barStyle=UIBarStyleBlackOpaque;
    [pickerToolbar sizeToFit];   
    NSMutableArray *barItems = [[NSMutableArray alloc] init];   
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(DatePickerDoneClick)];
    [barItems addObject:flexSpace];

    [pickerToolbar setItems:barItems animated:YES];       
    [pickerViewDate addSubview:pickerToolbar];
    [pickerViewDate addSubview:theDatePicker];
    [pickerViewDate  showInView:self.view];
    [pickerViewDate setBounds:CGRectMake(0,0,320, 464)];
}

-(IBAction)dateChanged{

    NSDateFormatter *FormatDate = [[NSDateFormatter alloc] init];
    [FormatDate setLocale: [[[NSLocale alloc]
                              initWithLocaleIdentifier:@"en_US"] autorelease]];
    [FormatDate setDateFormat:@"MM/dd/YYYY"];   
    SelectedTextField.text = [FormatDate stringFromDate:[theDatePicker date]];
}


-(BOOL)closeDatePicker:(id)sender{   
    [pickerViewDate dismissWithClickedButtonIndex:0 animated:YES];
    [pickerToolbar release];
    [pickerViewDate release];
    [SelectedTextField resignFirstResponder];   


    return YES;
}

-(IBAction)DatePickerDoneClick{   
    [self closeDatePicker:self];
    tableview.frame=CGRectMake(0, 44, 320, 416);

}

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

// returns the number of rows
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return 30;
    //    return [pickerViewArray count];
}

根据要求进行更改。这是我在类似应用程序中几乎使用的100%运行代码

快乐的编码......

答案 2 :(得分:1)

这是UIDatePicker的演示项目,带有取消/完成按钮:https://github.com/lenhhoxung86/CustomDatePicker

答案 3 :(得分:0)

另一个可自定义的UIToolbar和UIDatePicker项目。可能会有所帮助: https://github.com/anatoliyv/SMDatePicker