D
D
Darkness2020-09-28 11:00:48
Swift
Darkness, 2020-09-28 11:00:48

How to correctly implement the delegate in this case?

Purpose
When clicking on a custom button, in another custom UIView subclass, do something.

What I did
This is a custom button:

protocol CHNumberKeyboardModifyDelegate: class where Self: CHDotsContainer {
    func modify()
}
class CHNumberKeyboardButton: UIButton {
    weak var chNumberKeyboardModifyDelegate: CHNumberKeyboardModifyDelegate?
  
    @objc func touch(_ sender: UIButton!) {
        chNumberKeyboardModifyDelegate?.modify()
    }
}


This is a custom UIView where you need to do something when you click on the button above:
class CHDotsContainer: UIView, CHNumberKeyboardModifyDelegate {
    func modify() {
        print("touched")
    }
}


But the thing is, I don't know how to make the custom UIView subscribe to the delegate.
This does not work in the init class. As I understand it, because I create a button, but there is no button itself and therefore the delegate does not work.
let button = CHNumberKeyboardButton()
   button.chNumberKeyboardModifyDelegate = self


Question
: How should I proceed in this case?
Since I'm creating the buttons elsewhere, I also need to subscribe the UIView to the UIButton delegate.

To understand what buttons we are talking about, a screenshot:
Number buttons are custom buttons. And the dots above are custom UIView

5f719801aafd7554288628.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question