Should a UIView reference a UIViewController?

Some answers say Get a UIViewController from a UIView? - This is bad design practice.

But, LevelView , from Apple's own BubbleLevel Xcode project, has the property of assigning it to a viewController .

I think it’s best to define a protocol and designate the view controller as the view delegate, as you would with a UITableView or UITextField or UITextView .

But, I could be wrong, so my question is what is the recommended way for presenting communication with its view controller?

+6
source share
2 answers

Delegation (using a weak link) is a great way to represent communication with the controller. However, your opinion should not know that it is related to the controller. The view should only know that there is some kind of object that implements its delegate protocol and does not need to move the controller hierarchy or uses any attributes of its delegate that are not defined in the delegate protocol.

This allows views to remain very loosely connected to their controllers. You should be able to switch which object acts as the delegate of the view, or change which controller represents this view, without even changing the view itself.

+9
source

The best practice for UIView is to communicate with the ViewController using goals that can be defined from the ViewController using (IBAction) for methods and (IBOutlets) for variables. Whenever a user makes an event with a view, he can alert the controller through these IBActions, and the controller can (talk) to the view using IBOutlets

+2
source

Source: https://habr.com/ru/post/890958/


All Articles