Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView & Subview Delegates

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:

  1. The ViewController's (A) delegate implementation?
  2. The UIView's (B) delegate implementation?
  3. 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.

like image 949
David Pullar Avatar asked Sep 29 '15 04:09

David Pullar


People also ask

What is a UIView?

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.

What is a UIView service?

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.

What is setNeedsDisplay?

setNeedsDisplay()Marks the receiver's entire bounds rectangle as needing to be redrawn.


1 Answers

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:

  1. Create a protocol ViewDelegate and keep both View B and View C delegate methods in it.
  2. Create a property @property (nonatomic, weak) id <ViewDelegate> delegate; in View B.
  3. Create a property @property (nonatomic, weak) id <ViewDelegate> delegate; in View C.
  4. From ViewController A while instantiating View B set self as delegate. Like viewBObj.delegate = self.
  5. From View B while instantiating View C set self.delegate as delegate. Like viewBObj.delegate = self.delegate.
  6. This would make ViewController A responding to both View B and View C delegate events.
like image 132
Abhinav Avatar answered Nov 03 '22 04:11

Abhinav