我为日期选择器创建了一个单独的类。

import UIKit

protocol MYDatePickerViewControllerDelegate {
    func didFinishSelectingDate(selectedDate:NSDate)
}

class MYDatePickerViewController: UIViewController {

    @IBOutlet weak var containerView: UIView!
    @IBOutlet weak var datePicker: UIDatePicker!
    @IBOutlet weak var navigationBar: UINavigationBar!

    var datePickerDelegate : MYDatePickerViewControllerDelegate?
    var customTransitioningDelegate: MYFadeInFadeOutTransitioning? = MYFadeInFadeOutTransitioning()

    init(){
        super.init(nibName: "MyDatePickerViewController", bundle: NSBundle.mainBundle())

        transitioningDelegate = customTransitioningDelegate
        modalPresentationStyle = .Custom
    }
    convenience init(withSelectedDate date: NSDate) {
        self.init()

    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    deinit {
        customTransitioningDelegate = nil
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        setupViewControllerUI()
    }

    func setupViewControllerUI() {
        containerView.tag = kContainerViewTag

    }

    @IBAction func doneButtonTapped(sender: AnyObject) {
        datePickerDelegate?.didFinishSelectingDate(self.datePicker.date)
        print(self.datePicker.date)
        dismissViewControllerAnimated(true, completion: nil)
    }
    @IBAction func cancelButtonTapped(sender: AnyObject) {
        dismissViewControllerAnimated(true, completion: nil)
    }
}

我从另一个viewController呈现为
let timePickerViewController = MYDatePickerViewController()
presentViewController(timePickerViewController, animated: true, completion: nil)

它确实从屏幕底部显示如下

ios - 快速从DatePicker获取日期/时间-LMLPHP

现在,我试图在用户点击datePicker上的完成按钮时获取选定的日期。为此,我正在尝试创建一个代表并从那里获得选择的日期。但不能。
我已经将MYDatePickerViewController的委托包含到我的其他ViewController中,如下所示:

在我的viewDidLoad中
let datePicker = YASDatePickerViewController()
    datePicker.datePickerDelegate = self

然后
func didFinishSelectingDate(selectedDate: NSDate) {
    print(selectedDate)
}

但是我没有约会,请指导我。

最佳答案

从另一个viewController呈现为时尝试:

let timePickerViewController = MYDatePickerViewController()
timePickerViewController.datePickerDelegate = self
presentViewController(timePickerViewController, animated: true, completion: nil)

并使viewController符合您的协议:
class ViewController: UIViewController, MYDatePickerViewControllerDelegate {

休息看起来不错!

关于ios - 快速从DatePicker获取日期/时间,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39221260/

10-08 23:10
查看更多