addTarget 不触发任何事件

addTarget not firing any event

我有以下代码,但从未调用 doneButton 处理程序。知道可能出了什么问题吗?

我已经为选择器尝试了很多不同的格式,也尝试过带参数和不带参数。我还通过手动设置 userInteractionEnabled = true 确保 UIView 接收到触摸事件。提前感谢您的帮助!

*** 更新:我已经完全重构了代码,但行为相同:

我的协议:

import Foundation

protocol CustomDatePickerDelegate {
    func dataPickerDone(date: NSDate)
    func dataPickerChanged(date: NSDate)
}

我的class:

import Foundation
import UIKit

class CustomDatePicker{

var delegate: CustomDatePickerDelegate?
var datePicker : UIDatePicker = UIDatePicker()

func CreateDatePicker(sender: UITextField) {

    if (delegate is UIViewController)
    {
        let _self = delegate as! UIViewController

        let _view = UIView(frame: CGRectMake(0, 0, _self.view.frame.width, 220))

        let datePickerView  : UIDatePicker = UIDatePicker(frame: CGRectMake(0, 40, 0, 0))
        datePickerView.datePickerMode = UIDatePickerMode.Date


        let _toolbar = UIToolbar()
        let _doneButton = UIBarButtonItem(title: "DONE", style: UIBarButtonItemStyle.Done, target: self, action: #selector(CustomDatePicker.doneButton))
        _toolbar.translucent = false
        _toolbar.sizeToFit()

        _doneButton.accessibilityLabel = "DoneToolbar"
        let _spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)


        _toolbar.setItems([_spaceButton, _doneButton], animated: false)
        _toolbar.userInteractionEnabled = true
                    datePickerView.addTarget(self, action: #selector(CustomDatePicker.handleDatePicker), forControlEvents: UIControlEvents.ValueChanged)

        _view.addSubview(datePickerView)
        _view.addSubview(_toolbar)

        sender.inputView = _view
    }
}

@objc func handleDatePicker(sender:UIDatePicker) {
    delegate?.dataPickerChanged(datePicker.date)
}

@objc func doneButton(){
    delegate?.dataPickerDone(datePicker.date)
}

}

我如何使用它 ViewController:

let _dataPicker : CustomDatePicker = CustomDatePicker()
    _dataPicker.delegate = self
    _dataPicker.CreateDatePicker(dataUI)

func dataPickerDone(date: NSDate) {
    dataUI.text = DataFormatejada(date)
    dataUI.resignFirstResponder()
}

func dataPickerChanged(date: NSDate) {
    dataUI.text = DataFormatejada(date)
}

经过数小时的尝试错误,我找到了解决问题的方法。问题出在行中的 target:self 引用:

let _doneButton = UIBarButtonItem(title: "DONE", style: UIBarButtonItemStyle.Done, target: self, action: #selector(CustomDatePicker.doneButton))

_toolbar.userInteractionEnabled = true
                    datePickerView.addTarget(self, action: #selector(CustomDatePicker.handleDatePicker), forControlEvents: UIControlEvents.ValueChanged)

所以我所做的是在 MyDatePicker class 中创建一个新的 属性:

var instance : CustomDatePicker?

将这些 addTarget 方法更改为:

let _doneButton = UIBarButtonItem(title: "DONE", style: UIBarButtonItemStyle.Done, target: instance, action: #selector(CustomDatePicker.doneButton))
 _toolbar.userInteractionEnabled = true
                    datePickerView.addTarget(instance, action: #selector(CustomDatePicker.handleDatePicker), forControlEvents: UIControlEvents.ValueChanged)

我将 ViewController 中的它用作:

let _dataPicker : CustomDatePicker = CustomDatePicker()
    _dataPicker.delegate = self   <---- **this made the trick**
    _dataPicker.instance = _dataPicker
    _dataPicker.CreateDatePicker(dataUI)

但这并不优雅,它是针对更基本问题的补丁。为什么 self 没有提供对象的实例,而我必须在创建对象时手动将其传回?

谢谢!