【问题标题】:How to add UIDatePicker into UIAlertController iOS9如何将 UIDatePicker 添加到 UIAlertController iOS9
【发布时间】:2016-08-26 05:24:53
【问题描述】:

我尝试将 date piker 添加到 alertcontroller 但大小有一些问题。我不知道如何获得datepiker 的高度或只是init UIAlertController 的高度和日期piker。 所以我做了什么:

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"\n\n\n\n\n\n\n\n\n\n" preferredStyle:UIAlertControllerStyleActionSheet];

UIView *viewDatePicker = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
[viewDatePicker setBackgroundColor:[UIColor clearColor]];

CGRect pickerFrame = CGRectMake(0, 0, self.view.frame.size.width, 200);
UIDatePicker *picker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
[picker setDatePickerMode:UIDatePickerModeTime];

[viewDatePicker addSubview:picker];

[alertController.view addSubview:viewDatePicker];

NSDateFormatter *formate=[[NSDateFormatter alloc]init];
[formate setDateFormat:@"hh:mm a "];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                      handler:^(UIAlertAction * action) {

                                                          sinceDateString = [formate stringFromDate:picker.date];
                                                          [sinceDateButton setTitle:sinceDateString forState:UIControlStateNormal];

                                                      }];

[alertController addAction:defaultAction];
[self presentViewController:alertController animated:YES completion:nil];

我发现这个解决方案是同一个问题 UIAlertController 之一 *alertController = [UIAlertController alertControllerWithTitle:nil message:@"\n\n\n\n\n\n\n\n\n\n" preferredStyle:UIAlertControllerStyleActionSheet];

但我知道这是错误的解决方案,因为我看到了灰线。

【问题讨论】:

  • 即你当前选择日期的选择行,
  • 哦,对不起,我是 iOS 开发的新手。但是“\n\n\n”只有一个解决方案添加 UIAlertController 的高度呢?
  • \n 表示创建新行
  • ok 没问题,在 google 中搜索更多你总是得到最简单的答案,做得好

标签: ios objective-c uidatepicker uialertcontroller


【解决方案1】:

为什么没有为您的概念选择UIDatePicker,请参阅此example

\n 是换行符,将位置移动到下一行的开头

你的第二个问题在中心它显示了灰线,这意味着你的日期选择器选择区域。

在您的代码中,您在 10 次中使用了 \n\n\n\n\n\n\n\n,删除两个 \n\n 然后显示完美。

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"\n\n\n\n\n\n\n\n" preferredStyle:UIAlertControllerStyleActionSheet];

UIView *viewDatePicker = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,200)];
[viewDatePicker setBackgroundColor:[UIColor clearColor]];

CGRect pickerFrame = CGRectMake(0, 0, self.view.frame.size.width, 200);
UIDatePicker *picker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
[picker setDatePickerMode:UIDatePickerModeTime];

[viewDatePicker addSubview:picker];

[alertController.view addSubview:viewDatePicker];

NSDateFormatter *formate=[[NSDateFormatter alloc]init];
[formate setDateFormat:@"hh:mm a"];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                      handler:^(UIAlertAction * action) {

                                                          //                                                              sinceDateString = [formate stringFromDate:picker.date];
                                                          //                                                              [sinceDateButton setTitle:sinceDateString forState:UIControlStateNormal];

                                                      }];

[alertController addAction:defaultAction];
[self presentViewController:alertController animated:YES completion:nil];

我得到了类似的输出

【讨论】:

  • 检查更新答案,但在这里我的建议使用UIDatePicker
  • 感谢您的帮助!
【解决方案2】:

Apple 一直不鼓励将 UIDatePicker 添加到操作表。自 iOS 7 以来,Apple 引入了内联日期选择器的使用(查看它在日历应用中是如何完成的)。

如果您设法使用 UIAlertController 破解了一个解决方法,它可能会在未来的 iOS 版本中再次崩溃。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    • 2015-11-02
    • 2013-09-29
    相关资源
    最近更新 更多