UIPopover 中的 UIDatePicker

发布于 2024-12-03 14:37:35 字数 250 浏览 0 评论 0原文

我搜索了整个互联网(也许我有点夸张),寻找有关如何将 DatePicker 放入 UIPopover 并在 iPad 应用程序中显示它的教程。我尝试创建一个视图控制器,将日期选择器放入视图控制器中,然后:

self.popover = [[UIPopoverController alloc] initWithContentViewController:sa];

(sa 是我创建的视图控制器的名称),但这不起作用,应用程序崩溃了。有人可以帮忙吗?

I have searched the entire internet (maybe I am exaggerating a little) for a tutorial on how to put a DatePicker into a UIPopover and display that in an iPad application. I have tried creating a viewcontroller, placing the datepicker in the view controller and then:

self.popover = [[UIPopoverController alloc] initWithContentViewController:sa];

(sa is the name of the viewcontroller i created), but this doesn't work, and the app crashes. Can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘羡 2024-12-10 14:37:35

尝试使用下面的代码。它将正常工作:

Objective-C

- (IBAction)showDatePicker:(UIButton *)sender {
    UIDatePicker *datePicker = [[UIDatePicker alloc]init];//Date picker
    datePicker.frame = CGRectMake(0, 0, 320, 216);
    datePicker.datePickerMode = UIDatePickerModeDateAndTime;
    [datePicker setMinuteInterval:5];
    [datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];//need to implement this method in same class


    UIView *popoverView = [[UIView alloc] init];   //view
    popoverView.backgroundColor = [UIColor clearColor];
    [popoverView addSubview:datePicker];
    // here you can add tool bar with done and cancel buttons if required

    UIViewController *popoverViewController = [[UIViewController alloc] init];
    popoverViewController.view = datePicker;
    popoverViewController.view.frame = CGRectMake(0, 0, 320, 216);
    popoverViewController.modalPresentationStyle = UIModalPresentationPopover;
    popoverViewController.preferredContentSize = CGSizeMake(320, 216);
    popoverViewController.popoverPresentationController.sourceView = sender; // source button
    popoverViewController.popoverPresentationController.sourceRect = sender.bounds; // source button bounds
    //popoverViewController.popoverPresentationController.delegate = self;
    [self presentViewController:popoverViewController animated:YES completion:nil];
}
- (void)dateChanged:(UIDatePicker *)datePicker {
    NSLog(@"DATE :: %@", datePicker.date);
}

Swift 4.2

 @IBAction func showDatePicker(_ sender: UIButton) {
    let datePicker = UIDatePicker()//Date picker
    let datePickerSize = CGSize(width: 320, height: 216) //Date picker size
    datePicker.frame = CGRect(x: 0, y: 0, width: datePickerSize.width, height: datePickerSize.height)
    datePicker.datePickerMode = .dateAndTime
    datePicker.minuteInterval = 5
    datePicker.addTarget(self, action: #selector(dateChanged(_:)), for: .valueChanged)

    let popoverView = UIView()
    popoverView.backgroundColor = UIColor.clear
    popoverView.addSubview(datePicker)
    // here you can add tool bar with done and cancel buttons if required

    let popoverViewController = UIViewController()
    popoverViewController.view = popoverView
    popoverViewController.view.frame = CGRect(x: 0, y: 0, width: datePickerSize.width, height: datePickerSize.height)
    popoverViewController.modalPresentationStyle = .popover
    popoverViewController.preferredContentSize = datePickerSize
    popoverViewController.popoverPresentationController?.sourceView = sender // source button
    popoverViewController.popoverPresentationController?.sourceRect = sender.bounds // source button bounds
    popoverViewController.popoverPresentationController?.delegate = self // to handle popover delegate methods 
    self.present(popoverViewController, animated: true, completion: nil)

    }
    @objc func dateChanged(_ datePicker: UIDatePicker) {
        print("DATE :: \(datePicker.date)")
    }

如果您在视图控制器中分别实现以下委托,则相同的代码也适用于 iPhone

extension YourViewController : UIPopoverPresentationControllerDelegate {
    func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
        // Force popover style
        return UIModalPresentationStyle.none
    }
}

Try with below code. It will work fine:

Objective-C

- (IBAction)showDatePicker:(UIButton *)sender {
    UIDatePicker *datePicker = [[UIDatePicker alloc]init];//Date picker
    datePicker.frame = CGRectMake(0, 0, 320, 216);
    datePicker.datePickerMode = UIDatePickerModeDateAndTime;
    [datePicker setMinuteInterval:5];
    [datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];//need to implement this method in same class


    UIView *popoverView = [[UIView alloc] init];   //view
    popoverView.backgroundColor = [UIColor clearColor];
    [popoverView addSubview:datePicker];
    // here you can add tool bar with done and cancel buttons if required

    UIViewController *popoverViewController = [[UIViewController alloc] init];
    popoverViewController.view = datePicker;
    popoverViewController.view.frame = CGRectMake(0, 0, 320, 216);
    popoverViewController.modalPresentationStyle = UIModalPresentationPopover;
    popoverViewController.preferredContentSize = CGSizeMake(320, 216);
    popoverViewController.popoverPresentationController.sourceView = sender; // source button
    popoverViewController.popoverPresentationController.sourceRect = sender.bounds; // source button bounds
    //popoverViewController.popoverPresentationController.delegate = self;
    [self presentViewController:popoverViewController animated:YES completion:nil];
}
- (void)dateChanged:(UIDatePicker *)datePicker {
    NSLog(@"DATE :: %@", datePicker.date);
}

Swift 4.2

 @IBAction func showDatePicker(_ sender: UIButton) {
    let datePicker = UIDatePicker()//Date picker
    let datePickerSize = CGSize(width: 320, height: 216) //Date picker size
    datePicker.frame = CGRect(x: 0, y: 0, width: datePickerSize.width, height: datePickerSize.height)
    datePicker.datePickerMode = .dateAndTime
    datePicker.minuteInterval = 5
    datePicker.addTarget(self, action: #selector(dateChanged(_:)), for: .valueChanged)

    let popoverView = UIView()
    popoverView.backgroundColor = UIColor.clear
    popoverView.addSubview(datePicker)
    // here you can add tool bar with done and cancel buttons if required

    let popoverViewController = UIViewController()
    popoverViewController.view = popoverView
    popoverViewController.view.frame = CGRect(x: 0, y: 0, width: datePickerSize.width, height: datePickerSize.height)
    popoverViewController.modalPresentationStyle = .popover
    popoverViewController.preferredContentSize = datePickerSize
    popoverViewController.popoverPresentationController?.sourceView = sender // source button
    popoverViewController.popoverPresentationController?.sourceRect = sender.bounds // source button bounds
    popoverViewController.popoverPresentationController?.delegate = self // to handle popover delegate methods 
    self.present(popoverViewController, animated: true, completion: nil)

    }
    @objc func dateChanged(_ datePicker: UIDatePicker) {
        print("DATE :: \(datePicker.date)")
    }

Same code will work iPhone also if you implement below delegate respective in view controller

extension YourViewController : UIPopoverPresentationControllerDelegate {
    func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
        // Force popover style
        return UIModalPresentationStyle.none
    }
}
一页 2024-12-10 14:37:35

现在,既然您已经标记了 iPhone,那么可以清楚地假设您正在尝试在 iPhone 中使用 UIPopoverController,但是 UIPopoverController仅适用于 iPad。由于 iPhone 无法识别该控制器,因此会导致崩溃。

Now since you have tagged iPhone It can clearly be assumed that you are trying to use UIPopoverController in iPhone But UIPopoverController is available only for iPad. So it is resulting in a crash since iPhone doesn't recognize that controller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文