对象从一个类到另一个类的循环调用
我有A类和B类。现在我想在ClassB中调用ClassA的方法。所以我在ClassB中导入了ClassA并创建了它的一个对象并调用了该方法。现在我想在ClassA中调用ClassB的方法。我该怎么办呢。
I have ClassA and ClassB. Now i want to call a method of ClassA in ClassB.So i imported ClassA in ClassB and created an object of it and called that method.Now i want to call method of classB in classA. How can i do that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你的根本问题是循环导入。答案是停止在头文件中导入。如果 ClassA 需要从 ClassB 调用某些内容,则应将
#import "ClassB.h"
添加到ClassA.m
,不添加到ClassA.h
。如果 ClassB 需要调用 ClassA 中的某些内容,情况也是如此。 使用前向声明该类如果 ClassA 需要在其标头中引用 ClassB(例如 ClassB 类型的属性),那么您可以在
ClassA.h
顶部 。这告诉编译器存在一个名为ClassB
的类,并且您稍后将告诉它该类是什么。I assume your fundamental issue is a cyclic import. The answer to this is stop importing in your header file. If ClassA needs to call something from ClassB, then
#import "ClassB.h"
should be added toClassA.m
, not toClassA.h
. Same thing if ClassB needs to call something in ClassA. If ClassA needs to reference ClassB in its header (e.g. a property of type ClassB) then you can forward-declare the class usingat the top of your
ClassA.h
. This tells the compiler that there exists a class namedClassB
, and that you will tell it what that class is at some later time.