Answer the question
In order to leave comments, you need to log in
Why protocol in Swift?
Distinguished gentlemen of the pros.
Can you explain what is the meaning of delegation in Swift through protocol, if the execution of the same methods, the same UIView can be implemented by adding a reference of this object to the controller and executing its public methods as much as you like?!?!
Answer the question
In order to leave comments, you need to log in
Of course, UIView methods can be called from the controller, delegation is not needed for this.
Delegation should be the other way around, tell the controller that some event has occurred on the UIView (for example, the user tapped on the UITableView row) or request some data or values from the controller (for example, the title value for the UIPickerView row). At the same time, it is impossible to keep a reference to the controller in UIView, because there will be a retain cycle between UIView and UIViewController: each of them will keep a link to each other, and they will never release memory (here you can read more about this: https://developer.apple.com/library/prerelease/ios. ..
Of course, UIView can hold a weak reference to the controller, but in this case, this view will be rigidly attached to this controller and will work only with it. And the protocols were invented for this, that you can implement your controller in such a way that it satisfies the protocol, and the view would already work with this protocol. In the future, if you need to work with this view from another controller, just implement the same protocol in it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question