Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement multi-level delegate in Objective-C

I have 4 classes (views):
A, B, C and D

Class A calls B, B calls C, and C Calls D:
A > B > C > D

In class D I have implemented a delegate protocol and I want to catch the delegate event in class A.

How can I achieve this?

like image 251
Kassem Avatar asked Feb 16 '26 01:02

Kassem


1 Answers

There are multiple ways how you could achieve this. What's best in your case depends on the situation. Here are some ideas:

  1. You could implement the delegate protocol in all of those classes and simply pass it down the line.
  2. You could add an ivar to access class D from A and pass it directly (danger of spaghetti code!)
  3. If it's possible you could change your implementation, so that you only implement the delegate in A and handle it right there.
  4. A last resort could be using NSNotifications (not to be confused with NSUserNotifications in Mountain Lion). In your class A you post a notification to the default notification center and in class D you register to this notification and handle it as you want. Only use this approach though if nothing else works, because this can result in even worse code.
like image 160
DrummerB Avatar answered Feb 18 '26 15:02

DrummerB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!