将UIDatePicker添加到UIAlertView

我正在尝试添加dateselect器到警报视图。 我可以添加animation到屏幕基于一个button点击,我看到一个黑匣子,但没有dateselect器。

这是我到目前为止….

- (IBAction)showDatePicker { CGRect frame = CGRectMake(200, self.view.frame.size.height, self.view.frame.size.width, 0); //CGRectMake(225, 145, 260, 125); UIPickerView *datePicker = [[UIPickerView alloc] initWithFrame:frame]; UIAlertView *showDateAlert = [[UIAlertView alloc] initWithTitle:@"Enter the Code Date" message:@"Sample message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Update", nil]; [self.view addSubview:datePicker]; [UIView beginAnimations:@"slideIn" context:nil]; [datePicker setCenter:CGPointMake(datePicker.frame.origin.x, self.view.frame.size.height - datePicker.frame.size.height/2)]; [UIView commitAnimations]; } 

我发现一个似乎工作的例子,但我没有看到任何对话框或button在我的警报框,只是dateselect器本身。 我在这里错过了什么?

 - (IBAction)showDatePicker { CGRect frame = CGRectMake(225, self.view.frame.size.height, self.view.frame.size.width, 125); UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:frame]; datePicker = [[UIDatePicker alloc] init]; datePicker.datePickerMode = UIDatePickerModeDate; [datePicker setDate:[NSDate date]]; UIAlertView *alert; alert = [[UIAlertView alloc] initWithTitle:@"Enter the Code Date" message:@"Sample message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Update", nil]; alert.delegate = self; [alert addSubview:datePicker]; [alert show]; } 

我同意那些一直在说这不是实现这个目的的方法的评论者,因为几个原因。 首先,没有人喜欢有不断冒出的警戒线。 其次,并不适用于您的情况,最终可能会导致应用程序被拒绝。 苹果公司已经改变了UIAlertView类参考中的措辞,将其视图层级视为私有; 而且我们都知道苹果怎么觉得你在私底下偷偷摸摸。

来自UIAlertView类的参考:

UIAlertView类旨在按原样使用,不支持子类。 这个类的视图层次是私有的,不能被修改。

但是既然你说这是一个私人的应用程序,在这里。 UIAlertView只是一个UIView子类。 所以所有的框架和边界的规则仍然适用。 下面是一个基本的示例,说明如何调整选取器的边框和警报的边界以挤压dateselect器。

 // Create alert UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; // Show alert (required for sizes to be available) [alert show]; // Create date picker (could / should be an ivar) UIDatePicker *picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(10, alert.bounds.size.height, 320, 216)]; // Add picker to alert [alert addSubview:picker]; // Adjust the alerts bounds alert.bounds = CGRectMake(0, 0, 320 + 20, alert.bounds.size.height + 216 + 20); 

编辑

正如已经注意到的许多“如何添加一些UIAlertView视图的问题”的意见和新的答案,这种方法不再适用于iOS7和以上。 这应该被认为是一个根本不好的主意。

还要注意setValue:forKey:的用法setValue:forKey:在通常stream传的“解决scheme”中,不能修改Apple的私有视图层次结构的“问题”。 setValue:forKey:是吸引私人API扫描器兴趣的方法之一。 在UIAlertView文档和头文件中searchaccessoryView 。 这不是在文档中。 标题中唯一相关的项目是名为_accessoryView的ivar,标记为@private

再次,对于一个内部或私人应用程序,我想这没关系,只是没关系。

这是我的工作(使用iOS7)。 在[alertView show]之前调用它:

 [alertView setValue:customContentView forKey:@"accessoryView"]; 

例:

 UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"TEST" message:@"subview" delegate:nil cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil]; UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)]; v.backgroundColor = [UIColor yellowColor]; [av setValue:v forKey:@"accessoryView"]; [av show]; 

来源: UIAlertView自定义中的iOS 7 UIDatePicker

你不会喜欢这个答案,但我的build议是:不要这样做! UIAlertView不适合您添加您自己的子视图; 你发现做这些事的例子就是错误行为的例子。 创build您自己的视图,包含您想要的选取器,文本,button等,并以某种方式呈现它。 例如,你可以通过它自己的视图控制器,例如presentModalViewController:animated:模拟地呈现它presentModalViewController:animated:presentViewController:animated:completion: 如果你在iPad上,你可以使用popover(如果这很重要的话,它可以是模态的)。

这是在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]; 

在Swift中:

  let myDatePicker: UIDatePicker = UIDatePicker() // setting properties of the datePicker myDatePicker.timeZone = NSTimeZone.localTimeZone() myDatePicker.frame = CGRectMake(0, 15, 270, 200) let alertController = UIAlertController(title: "\n\n\n\n\n\n\n\n", message: nil, preferredStyle: UIAlertControllerStyle.Alert) alertController.view.addSubview(myDatePicker) let somethingAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil) let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil) alertController.addAction(somethingAction) alertController.addAction(cancelAction) self.presentViewController(alertController, animated: true, completion:{})