I have 4 classes (views):
A, B, C and D
我有4个类(视图):A、B、C和D
Class A calls B, B calls C, and C Calls D:
A > B > C > D
A类调用B, B调用C, C调用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.
在类D中,我实现了一个委托协议,我想捕获类a中的委托事件。
How can I achieve this?
我如何做到这一点?
2 个解决方案
#1
10
There are multiple ways how you could achieve this. What's best in your case depends on the situation. Here are some ideas:
有多种方法可以实现这一点。你的情况最好是什么取决于情况。这里有一些建议:
- You could implement the delegate protocol in all of those classes and simply pass it down the line.
- 您可以在所有这些类中实现委托协议,并简单地将其传递到行中。
- You could add an ivar to access class D from A and pass it directly (danger of spaghetti code!)
- 您可以添加一个ivar来从A访问D类并直接传递它(通心粉代码的危险!)
- If it's possible you could change your implementation, so that you only implement the delegate in A and handle it right there.
- 如果可能的话,您可以更改您的实现,因此您只需要在A中实现委托并在那里处理它。
- A last resort could be using
NSNotifications
(not to be confused withNSUserNotifications
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. - 最后一种方法是使用nsnotification(不要与Mountain Lion中的NSUserNotifications混淆)。在类A中,您将通知发布到默认通知中心,在类D中,您注册到该通知并按自己的意愿处理它。只有在其他方法不起作用时才使用这种方法,因为这会导致更糟糕的代码。
#2
0
Assign ClassD member delegate as ClassA object.
将ClassD成员委托分配为ClassA对象。
Let me explain
让我解释一下
If you have a UITableView in ClassD and want to trigger the ClassD tableView Delegates in ClassA, let assign the tableView delegate as ClassA object and implement all the UITableView delegates in ClassA.
如果您在ClassD中有一个UITableView,并且想要触发ClassA中的ClassD tableView委托,那么让我们将tableView委托赋值为ClassA对象,并实现ClassA中的所有UITableView委托。
Eg: ClassD_tableView.delegate = classAObj;
例如:ClassD_tableView.delegate = classAObj;
Note: Implement the ClassD tableView delegate methods in Class A and Do not create a new object of ClassA from inside ClassD. you can get the parent Class A in ClassD through passing the parameter or using UIResponder.
注意:在类A中实现ClassD tableView委托方法,不要在ClassD中创建ClassA的新对象。可以通过传递参数或使用UIResponder在ClassD中获取父类A。
#1
10
There are multiple ways how you could achieve this. What's best in your case depends on the situation. Here are some ideas:
有多种方法可以实现这一点。你的情况最好是什么取决于情况。这里有一些建议:
- You could implement the delegate protocol in all of those classes and simply pass it down the line.
- 您可以在所有这些类中实现委托协议,并简单地将其传递到行中。
- You could add an ivar to access class D from A and pass it directly (danger of spaghetti code!)
- 您可以添加一个ivar来从A访问D类并直接传递它(通心粉代码的危险!)
- If it's possible you could change your implementation, so that you only implement the delegate in A and handle it right there.
- 如果可能的话,您可以更改您的实现,因此您只需要在A中实现委托并在那里处理它。
- A last resort could be using
NSNotifications
(not to be confused withNSUserNotifications
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. - 最后一种方法是使用nsnotification(不要与Mountain Lion中的NSUserNotifications混淆)。在类A中,您将通知发布到默认通知中心,在类D中,您注册到该通知并按自己的意愿处理它。只有在其他方法不起作用时才使用这种方法,因为这会导致更糟糕的代码。
#2
0
Assign ClassD member delegate as ClassA object.
将ClassD成员委托分配为ClassA对象。
Let me explain
让我解释一下
If you have a UITableView in ClassD and want to trigger the ClassD tableView Delegates in ClassA, let assign the tableView delegate as ClassA object and implement all the UITableView delegates in ClassA.
如果您在ClassD中有一个UITableView,并且想要触发ClassA中的ClassD tableView委托,那么让我们将tableView委托赋值为ClassA对象,并实现ClassA中的所有UITableView委托。
Eg: ClassD_tableView.delegate = classAObj;
例如:ClassD_tableView.delegate = classAObj;
Note: Implement the ClassD tableView delegate methods in Class A and Do not create a new object of ClassA from inside ClassD. you can get the parent Class A in ClassD through passing the parameter or using UIResponder.
注意:在类A中实现ClassD tableView委托方法,不要在ClassD中创建ClassA的新对象。可以通过传递参数或使用UIResponder在ClassD中获取父类A。