ios - 使用闭包作为按钮的 Action

标签 ios swift closures

背景

我必须在我的应用程序中创建一些日期时间选择器,所以我正在考虑创建一个Utility类,它将返回一个可重用的UIDateTimePicker 我将在整个应用程序中使用它。每个选择器在工具栏

中都有一个取消完成按钮

问题 对于点击取消完成时应该发生的 Action ,我正在考虑使用这样的闭包:

func createDatePicker(isDateTime: Bool, doneAction: ()->(), cancelAction: ()->()) -> UIDatePicker {

    // Code for setting up the dateTime picker

    // Deciding the picker mode for the picker
    dateTimePicker.datePickerMode = isDateTime ? UIDatePicker.Mode.dateAndTime : UIDatePicker.Mode.date

    // Creating a toolbar for the Picker

    let toolbar = UIToolbar()
    toolbar.sizeToFit()

    // Creating buttons for toolbar
    let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(doneAction)) // ---- ERROR -----

    let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)

    let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: #selector(cancelAction)) // ---- ERROR -----

    // Adding buttons to the toolbar
    toolbar.setItems([cancelButton,spaceButton,doneButton], animated: false)

    // Adding the Toolbar to the dateTimePicker and returning the dateTimePicker

}

现在,当我尝试将闭包添加为按钮的操作时出现问题,因为它们不是选择器(附有 @objc)

我得到的错误是

Argument of '#selector' cannot refer to parameter 'doneAction'

如何解决这个问题,或者是否有另一种方法将要执行的代码(在取消和完成时)从主(ViewController)类传递给这个实用程序类

最佳答案

如果我对您的理解正确,您可以通过向您的类(class)添加委托(delegate)来解决此问题。这是一些如何做的例子。希望对你有帮助

    protocol DoneCancelAction {

    func doneAction()
    func cancelAction()

}

class DateTimePicker: UIDatePicker {

    var dateTimePicker: DateTimePicker!
    var delegate: DoneCancelAction? = nil

    func createDatePicker(isDateTime: Bool) -> UIDatePicker {

        // Code for setting up the dateTime picker

        // Deciding the picker mode for the picker
        dateTimePicker.datePickerMode = isDateTime ? UIDatePicker.Mode.dateAndTime : UIDatePicker.Mode.date

        // Creating a toolbar for the Picker

        let toolbar = UIToolbar()
        toolbar.sizeToFit()

        // Creating buttons for toolbar
        let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.done, target: self, action: #selector(doneAction)) // ---- ERROR -----

        let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)

        let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItem.Style.plain, target: self, action: #selector(cancelAction)) // ---- ERROR -----

        // Adding buttons to the toolbar
        toolbar.setItems([cancelButton,spaceButton,doneButton], animated: false)

        // Adding the Toolbar to the dateTimePicker and returning the dateTimePicker


        return dateTimePicker

    }

    @objc func doneAction() {
        delegate?.doneAction()
    }

    @objc func cancelAction() {
        delegate?.cancelAction()
    }

}

class MyVC: UIViewController, DoneCancelAction {

    var dateTimePicker: DateTimePicker!

    override func viewDidLoad() {
        super.viewDidLoad()
        dateTimePicker.delegate = self
    }

    func doneAction() {
        // your code
    }

    func cancelAction() {
        // your code
    }

}

关于ios - 使用闭包作为按钮的 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59280332/

相关文章:

闭包中的 javascript 原型(prototype)和 "this"访问

ios - 以编程方式在 collectionView swift 中无限滚动

ios - 我们是否需要为每个应用程序更新一个新的 APNS 证书?

ios - 从 Firebase 检索数据等于意外的 nil - swift

swift - 如何取消接收器内的组合订阅?

swift - iOS : throttle bandwidth of e. g。阿拉莫菲尔

validation - 如何在 Symfony2 的验证组件中使用闭包?

rust - 如何通过原始指针将闭包作为参数传递给 C 函数?

ios - 如何将文本阴影应用于 UITextView?

ios - 在 iOS 中实现新的 "Card"样式模态视图的真实名称和方法是什么?