iOS 中弹出的 UIDatePickerView

UIDatePickerView In Popup in iOS

尝试将 UIDatePickerView 显示为 dialog box,就像在 Android 中显示的那样。我试图在 UIAlertView 上添加它,但无法添加。

有人试过吗?

AddSubview is not available in UIAlertView in iOS7

UIAlertView class 旨在按原样使用,不支持子classing。 此 class 的视图层次结构是私有的,不得修改。

你应该使用 UIAlertViewcontoller。这是代码:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"\n\n\n\n\n\n\n\n\n\n\n" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIDatePicker *picker = [[UIDatePicker alloc] init];
[picker setDatePickerMode:UIDatePickerModeDate];
[alertController.view addSubview:picker];
[alertController addAction:({
UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"OK");
NSLog(@"%@",picker.date);
}];
action;
})];
UIPopoverPresentationController *popoverController = alertController.popoverPresentationController;
popoverController.sourceView = sender;
popoverController.sourceRect = [sender bounds];
[self presentViewController:alertController  animated:YES completion:nil];

希望对您有所帮助....

在按钮操作中我设置了日期选择器

ViewController.m

#import "ViewController.h"

@interface ViewController ()
{
  UIDatePicker *datepicker;
}

@end

@implementation ViewController         



-(IBAction) showDatePicker:(id)sender
{
  UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"\n\n\n\n\n\n\n" message:nil preferredStyle:UIAlertControllerStyleAlert];
  datepicker = [[UIDatePicker alloc]init];  
  [datepicker setDatePickerMode:UIDatePickerModeDateAndTime];
  [datepicker setDatePickerMode:UIDatePickerModeDate];
  [datepicker addTarget:self action:@selector(getPickerValue:) forControlEvents:UIControlEventValueChanged];

  [alertController addAction:({
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"Done" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"The selected or picked datePicker value is - %@",datepicker.date);
        txtfldGetDatePickerValue.text = [self getStringDateFromDate:datepicker.date];
    }];
    action;
})];

[alertController.view addSubview:datepicker];
[self presentViewController:alertController animated:YES completion:nil];
}

-(void)getPickerValue:(UIDatePicker *)pickerDate
{
  txtfldGetDatePickerValue.text = [self getStringDateFromDate:pickerDate.date];
}

-(NSString *)getStringDateFromDate:(NSDate *)pickerDate
{
  NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
  [formatter setDateFormat:@"dd/MM/yyyy"];
  return [formatter stringFromDate:pickerDate];
}

我尝试了很多解决方案并得到了输出 finally.Since 我没有正确给出答案 我想为您提供最佳答案 question.I 用谷歌搜索了很多 answer.Even 我创建了示例项目实现 this.I 没有使用动作 sheet 和 popoverpresentation 视图 controller.Simply 我使用了 UIAlertViewController 编码。

UIDatePicker to action sheet was discouraged by Apple all along

这会起作用..检查 code.This 是否会在 ios7 及更高版本

中起作用
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Select Date" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
   UIDatePicker *picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(10, alert.bounds.size.height, 320, 216)];
[alert addSubview:picker];
alert.bounds = CGRectMake(0, 0, 320 + 20, alert.bounds.size.height + 216 + 20);
[alert setValue:picker forKey:@"accessoryView"];
[alert show];

为了在 Phone 中显示 Popover 只需创建 UIPopoverController 的类别 代码:

//  UIPopoverController+iPhone.h
#import <UIKit/UIKit.h>

@interface UIPopoverController (iPhone)
+ (BOOL)_popoversDisabled;
@end
//  UIPopoverController+iPhone.m


#import "UIPopoverController+iPhone.h"

@implementation UIPopoverController (iPhone)
+ (BOOL)_popoversDisabled {
    return NO;
}
@end

这一定能帮到你。