Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

ios - UIDatePicker in UIPopover

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...