I have a CustomView UIView class that represents my screenUI in which I programatically create and position the UI elements, among which multiple UITextFields and a UITableView.
I also have a UIViewController and in it’s loadView which I assign my CustomView to self.view.
I do this in order to best keep the cohesion of the view and the controller.
My question is how can I set the delegate of the UITextFields and UITableView inside the CustomView to the managing UIViewController?
It is my understanding that the UITextFieldDelegate and all other protocol methods should be implemented in the Controller and not the View, but I would like to avoid setting the delegate of the items in the view controller (e.g. textField1.delegate = self in the UIViewController class instead of textField1.delegate = <value for setting UIViewController as delegate> in my CustomView class) in order to keep my code cleaner.
Thank yous.
You could have a
delegateproperty on your custom view:In the delegate setter you could forward responsibilities:
You could have more than 1 type of delegate on your custom view, it depends on you needs, but one implementing all the protocols should usually suffice.