【问题标题】:UIDatePickerView In Popup in iOSiOS弹出窗口中的UIDatePickerView
【发布时间】:2016-11-18 13:09:48
【问题描述】:

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

有人试过吗?

【问题讨论】:

  • 如果点击alertbox中的ok按钮是否需要打开datePicker?
  • 不,我的屏幕上有按钮。点击该按钮,我需要 UIDatePicker 作为弹出窗口,如 UIAlertView,我可以在其中选择日期。有我的问题吗?

标签: ios iphone ios5


【解决方案1】:

AddSubview 在 iOS7 的 UIAlertView 中不可用

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

您应该使用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];

希望这对你有帮助....

【讨论】:

    【解决方案2】:

    这将工作..检查代码。这将在 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];
    

    【讨论】:

      【解决方案3】:

      要在手机中显示弹出框,只需创建 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
      

      这肯定会帮助你。

      【讨论】:

      • @surendrasheoran 只需导入您想使用的类别
      【解决方案4】:

      在按钮操作中我设置了 datePicker

      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];
      }
      

      我尝试了很多解决方案,最终得到了输出。由于我没有正确给出答案,我想为您的问题提供最佳答案。我用谷歌搜索了很多答案。即使我创建了示例项目来实现这一点。我没有使用操作表和 popoverpresentation 视图控制器。我只是使用了 UIAlertViewController 的编码。

      UIDatePicker to action sheet was discouraged by Apple all along

      【讨论】:

      • 它将在特定位置显示 UIDatepicker,而不是像弹出窗口。
      • 你需要为 iPhone 或 iPad 实现这个吗?
      • 在 iPhone 中。 iPad为此提供了UIPopoverViewController。
      猜你喜欢
      • 2011-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多