addTarget没有触发任何事件

时间:2016-04-16 21:00:36

标签: ios swift addtarget

我有以下代码,永远不会调用doneButton处理程序。知道可能出现什么问题吗?

我为选择器尝试了许多不同的格式,也有或没有参数。我还通过手动设置UIView确保userInteractionEnabled = true收到了触摸事件。感谢您提前提供任何帮助!

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

我的协议:

import Foundation

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

我的班级:

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

1 个答案:

答案 0 :(得分:0)

经过几个小时的试错后,我找到了解决问题的方法。问题在于行中的 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类中创建一个新属性:

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 没有提供对象的实例,我必须在创建对象时手动将其传回去?

谢谢!