Функция не вызывается с помощью делегата протокола и быстрого просмотра контроллера - PullRequest
0 голосов
/ 05 мая 2020

Мне нужно было делегировать действие щелчка для моего UIView класса моему UIViewController классу, поскольку swift не поддерживает множественное наследование классов. Поэтому я хотел, чтобы при нажатии кнопки в моем подвиде вызывалась функция в моем классе ViewController. Я использую protocol delegate для достижения этого, но при нажатии моей кнопки это не работает для меня, так как функция не вызывается. Пожалуйста, помогите мне. Фрагмент кода будет весьма признателен.

ViewController

 var categoryItem: CategoryItem! = CategoryItem() //Category Item
 private func setupExplore() {
//assign delegate of category item to controller
self.categoryItem.delegate = self
      }
//function to be called
extension BrowseViewController: ExploreDelegate {
    func categoryClicked(category: ProductCategory) {
        print("clicked")
        let categoryView = ProductByCategoryView()
        categoryView.category = category
        categoryView.modalPresentationStyle = .overCurrentContext
        self.navigationController?.pushViewController(categoryView, animated: true)
    }

}

Explore.swift (subview)

    import UIKit

    protocol ExploreDelegate:UIViewController {
        func categoryClicked(category: ProductCategory)
    }

    class Explore: UIView {
 var delegate: ExploreDelegate?

    class CategoryItem: UIView {
    var delegate: ExploreDelegate?
    var category: ProductCategory? {
        didSet {
            self.configure()
        }
    }
    var tapped: ((_ category: ProductCategory?) -> Void)?

    func configure() {
        self.layer.cornerRadius = 6
        self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.categoryTapped)))
        self.layoutIfNeeded()
    }

    @objc func categoryTapped(_ sender: UIGestureRecognizer) {
        delegate?.categoryClicked(category: ProductCategory.everything)
        self.tapped?(self.category)
    }
}
...