I just wanted to clear up some confusion that I have with the delegate pattern that should be constructed when there are multiple UIViews and Subviews of these views. To make it clear, let's define some variables.
Let us define these objects as:
- ViewController
A
- UIView
B
- Subview
C
Now, I understand how the delegation pattern works (I think), although I am unsure how to implement this pattern in nested UIViews. Some questions I have are:
Should C
contain a delegate implemented by it's super view (B
)?
And if yes, should B
then pass this information to it's delegate ViewController (A
)?
Here's a scenario, let's say C
has a UITextView, this text view's height is determined by a string fetched from an API service. B
does not have access to the API since this job should be done via the ViewController (A
).
Should C
then contain a delegate which points to:
- The ViewController's (
A
) delegate implementation?- The UIView's (
B
) delegate implementation?- Other?
If the answer is ( 2 ) then should B
then call the ViewController (A
) and pass this information via a chain of events?
Here's a small visual:
A <IBDelegate>
<--- B <ICDelegate>
<--- C
calls Delegate.OnApiComplete(float height);
What is the "Delegate" in this case? (ICDelegate
or IBDelegate
). And what are the chain of events?
I am just trying to avoid any unnecessary logic to seep into the UIView's when the responsibility should be on the controller.
I understand that you can solve most of these scenario's with a shared object between UIViews, but when it comes to network services, these values need to be retrieved via some sort of callback.
I further clarification is needed, let me know. Any help is greatly appreciated.
UIView can be defined as an object by using which we can create and manage the rectangular area on the screen. We can have any number of views inside a view to create a hierarchical structure of the UIViews. The UIView is managed by using the methods and properties defined in the UIView class that inherits UIKit.
The UIView class is a concrete class that you can instantiate and use to display a fixed background color. You can also subclass it to draw more sophisticated content.
setNeedsDisplay()Marks the receiver's entire bounds rectangle as needing to be redrawn.
So, you have situation like:
ViewController A --> View B --> View C
I would try to ensure that my ViewController A
takes decisions both for View B
& View C
like this:
ViewDelegate
and keep both View B
and View C
delegate methods in it.@property (nonatomic, weak) id <ViewDelegate> delegate;
in View B
.@property (nonatomic, weak) id <ViewDelegate>
delegate;
in View C
.ViewController A
while instantiating View B
set self
as delegate. Like viewBObj.delegate = self
.View B
while instantiating View C
set self.delegate
as delegate. Like viewBObj.delegate = self.delegate
.ViewController A
responding to both View B
and View C
delegate events.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With