简体   繁体   中英

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.

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.

Delegate are function pointers. Using it, one can call another class' function easily.

To create delegate, common procedure is to, first create protocol and add relevant methods in it (in the class you want to initiate delegate method). This methods can be implemented by class that adopts protocol.

You also need to create generic property of protocol type called delegate property. This will be assigned to instance of class that conforms to protocol.

In your case, class B and class C has some protocols defined in it. Here, B conforms class C's protocol and class A conforms class B's protocol.

Now, class B has object defined of class c in it. In class B, here we need to assign class C's delegate to instance of B(self). (now in class c, delegate property contains instance of B and one can easily call protocol method implemented in class B from class C).

The same scenario happen in class A and B where one can call method (defined in class B's protocol) of class A from class B.

Below is overview of implementation of delegate chain through A -> B -> C.

Class A

Conforms protocol B

It has object of class B

Assign instance of class A(self) to delegate property of class B

In class A, implement protocol methods defined in class B

Class B

Define protocol with methods

Define property of generic type that act as delegate instance

Conforms protocol C

It has object of class C

Assign instance of class B(self) to delegate property of class C

In class B, implement protocol methods defined in class C

Class C

Define protocol with methods

Define property of generic type that act as delegate instance

I hope this will help you understand how delegate works in iOS.

Now in you case, you can conforms protocol of class B and class C into class A (if you don't want any modification into Class B and simply call upper layer's delegate methods).

See below overview

Class A

Conforms protocol B

It has object of class B

Assign instance of class A(self) to delegate property of class B

In class A, implement protocol methods defined in class B

Conforms protocol C

It has object of class C through object of class B

Assign instance of class A(self) to delegate property of class C

In class A, implement protocol methods defined in class C

Class B

Define protocol with methods

Define property of generic type that act as delegate instance

Class C

Define protocol with methods

Define property of generic type that act as delegate instance

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM