实施委托协议

Implementing Delegate Protocol

我正在尝试实施委托协议,但我显然做错了。我正在使用 cocoapod:https://cocoapods.org/pods/SMDatePicker,我曾尝试联系开发人员寻求帮助,但到目前为止他从未回复过我。

添加并安装 pod 后,我将 .swift 文件添加到我的项目中,并添加了以下显示日期选择器的方法:

class CalculatorViewController: UIViewController, SMDatePickerDelegate {

@IBOutlet weak var lblDate: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    // Initialize
    let tap = UITapGestureRecognizer(target: self, action: #selector(CalculatorViewController.tapFunction))
    lblDate.isUserInteractionEnabled = true
    lblDate.addGestureRecognizer(tap)

    // Set label date
    let dt = Date()
    lblDate.text = dt.asString(style: .short)
}

@IBAction func datePicker(picker: SMDatePicker, didPickDate date: NSDate) {
    let picked = date as Date
    lblDate.text = picked.asString(style: .short)
}

@IBAction func tapFunction(sender: UITapGestureRecognizer) {
    var picker: SMDatePicker = SMDatePicker()
    picker.pickerMode  = .date
    picker.showPickerInView(view, animated: true)
    print("tap working")
}

func datePicker(_ picker: SMDatePicker, didPickDate date: Date) {
    let picked = date
    print(date)
}

}

我正在尝试从日期选择器中获取结果。根据pod,各种方法如下:

 // Initialize
var picker: SMDatePicker = SMDatePicker()
// Set delegate
picker.delegate = self
You have SMDatePickerDelegate protocol to handle picker’s events. Here are list:
datePickerWillAppear(picker: SMDatePicker)
datePickerDidAppear(picker: SMDatePicker)
datePicker(picker: SMDatePicker, didPickDate date: NSDate)
datePickerDidCancel(picker: SMDatePicker)
datePickerWillDisappear(picker: SMDatePicker)
datePickerDidDisappear(picker: SMDatePicker)

我的问题是我不确定如何实现它,任何帮助将不胜感激。

首先,向 CalculatorViewController 添加一个引用您 storyboard/XIB 中的日期选择器的出口。

然后,在 viewDidLoad() 中,将您的 class 指定为其委托人。

@IBOutlet weak var datePicker: SMDatePicker!

override func viewDidLoad() {
   self.datePicker.delegate = self
}

您添加为其委托的 class 必须符合 SMDatePickerDelegate 协议,您的示例也是如此。

然后,只需将方法添加到您的 class 即可对其事件作出反应。例如:

func datePicker(picker: SMDatePicker, didPickDate date: NSDate) {
    debugPrint("date picked! \(date)")
}

func datePickerDidCancel(picker: SMDatePicker) {
    debugPrint("date picker cancelled")
}

您从未设置 SMDatePickerdelegate。另外,您有两个 didPickDate 的演绎版。 Swift 中的正确语法是 datePicker(_:didPickDate:),其中第二个参数是 Date,而不是 NSDate

因此:

class ViewController: UIViewController {
    @IBOutlet weak var dateLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Initialize
        let tap = UITapGestureRecognizer(target: self, action: #selector(didTapLabel(_:)))
        dateLabel.isUserInteractionEnabled = true
        dateLabel.addGestureRecognizer(tap)

        // Set label date
        let now = Date()
        dateLabel.text = now.asString(style: .short)
    }

    @objc func didTapLabel(_ gesture: UITapGestureRecognizer) {
        let picker = SMDatePicker()
        picker.delegate = self                        // Don’t forget to set the delegate
        picker.pickerMode  = .date
        picker.showPickerInView(view, animated: true)
        print("tap working")
    }
}

extension ViewController: SMDatePickerDelegate {
    func datePicker(_ picker: SMDatePicker, didPickDate date: Date) {
        dateLabel.text = date.asString(style: .short)
    }
}

我个人会将 SMDatePickerDelegate 方法放在自己的 extension 中,只是为了保持代码的美观和条理。此外,您的点击手势识别器可以只声明为 @objc,而不是 @IBAction(因为后者表明您正在将其连接到 Interface Builder 中的操作(因此是 IB),而你只是通过 Objective-C #selector 模式连接它。