UIActionsheet Picker中的日期和时间

时间:2013-04-15 10:22:41

标签: ios objective-c ipad uiactionsheet actionsheetpicker

您好,我需要在仅限iPad的应用上显示uiactionsheet中的日期和时间。

我的google / sof搜索引导我访问以下资源https://github.com/TimCinel/ActionSheetPicker

ActionSheetDatePicker *actionSheetPicker = [[ActionSheetDatePicker alloc] initWithTitle:@"" datePickerMode:UIDatePickerModeDateAndTime selectedDate:[NSDate date] target:self action:@selector(onDateSelected:element:) origin:sender];
actionSheetPicker.hideCancel = YES;
[actionSheetPicker showActionSheetPicker];

除了我的生活之外它很有效我找不到我可以设置最小,最大日期和间隔的地方。 如果有人使用过这个可能会有所帮助。谢谢

4 个答案:

答案 0 :(得分:1)

 UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:@"Date Picker" 
                                                  delegate:self
                                         cancelButtonTitle:@"Cancel"
                                    destructiveButtonTitle:nil
                                         otherButtonTitles:nil];

    // Add the picker
    UIDatePicker *pickerView = [[UIDatePicker alloc] init];
    pickerView.datePickerMode = UIDatePickerModeDateAndTime;
    [menu addSubview:pickerView];
    [menu showInView:self.view];    
    [menu setBounds:CGRectMake(0,0,320, 300)];

    CGRect pickerRect = pickerView.bounds;
    pickerRect.origin.y = -100;
    pickerView.bounds = pickerRect;

以上代码可用于将UIDatePicker添加到UIActionSheet。在上面的代码中,您可以使用UIDatePicker显示日期和时间。

答案 1 :(得分:1)

这里我使用的是Actionsheet + UIDatePicker for iPhone / IPod,但是对于iPAD使用POPOVERCONTROLLER + UIDATEPICKER。

- (IBAction)showAction:(id)sender
{

aac = [[UIActionSheet alloc] initWithTitle:@""
                                                 delegate:self
                                        cancelButtonTitle:nil
                                   destructiveButtonTitle:nil
                                        otherButtonTitles:nil];

self.dtPicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];


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

NSMutableArray *barItems = [[NSMutableArray alloc] init];


UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(DatePickerCancelClick:)];
[barItems addObject:cancelBtn];

UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(DatePickerDoneClick:)];

[barItems addObject:doneBtn];


[pickerDateToolbar setItems:barItems animated:NO];

if (IS_IPHONE) {

    [aac addSubview:pickerDateToolbar];
    [aac addSubview:dtPicker];
    [self.view addSubview:aac];
}
else {
    UIView *view = [[UIView alloc] init];

    [view addSubview:pickerDateToolbar];
    [view addSubview:dtPicker];

    UIViewController *vc = [[UIViewController alloc] init];
    [vc setView:view];
    [vc setContentSizeForViewInPopover:CGSizeMake(320, 260)];

    popover = [[UIPopoverController alloc] initWithContentViewController:vc];
    popover.delegate = self;



    [popover presentPopoverFromRect:myButton.bounds inView:myButton
           permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];


}
}

//希望你明白了。感谢

答案 2 :(得分:1)

根据ibiren的UIDatePicker in UIPopover主题建议,我最终使用了这段代码。

UIViewController* popoverContent = [[UIViewController alloc] init]; //ViewController

UIView *popoverView = [[UIView alloc] init];   //view
popoverView.backgroundColor = [UIColor blackColor];

UIDatePicker *datePicker=[[UIDatePicker alloc]init];//Date picker
datePicker.frame=CGRectMake(0,44,320, 216);
datePicker.datePickerMode = UIDatePickerModeDateAndTime;
[datePicker setMinuteInterval:5];
[datePicker setTag:10];
[datePicker addTarget:self action:@selector(Result) forControlEvents:UIControlEventValueChanged];
[popoverView addSubview:datePicker];

popoverContent.view = popoverView;
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
popoverController.delegate=self;
[popoverContent release];

[popoverController setPopoverContentSize:CGSizeMake(320, 264) animated:NO];
[popoverController presentPopoverFromRect:tempButton.frame inView:self.view     permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
//tempButton.frame where you need you can put that frame

答案 3 :(得分:-1)

我知道这个答案迟到了。但是对于想要使用这个库的人来说...... 新版ActionSheetDatePicker具有以下属性:

NSDate *minimumDate;
NSInteger minuteInterval;
NSCalendar *calendar;
NSTimeZone *timeZone;
NSLocale *locale;

允许您根据需要修改DatePicker的基本选项。