Objective c 从父类调用方法

Objective c 从父类调用方法,objective-c,Objective C,我已经从另一个类中初始化了一个自定义类,然后我想从自定义类中运行一个函数,当它完成时,从初始化它的类中调用一个方法 // classA.m -(void)methodA { // do some complicated stuff if (result) { [classB methodB]; } } // classB.m -(void)methodA { classAInstance = [[classA alloc] init]

我已经从另一个类中初始化了一个自定义类,然后我想从自定义类中运行一个函数,当它完成时,从初始化它的类中调用一个方法

// classA.m

-(void)methodA {
    // do some complicated stuff
    if (result) {
        [classB methodB];
    }
}

// classB.m    

-(void)methodA {
    classAInstance = [[classA alloc] init];
    [classAInstance methodA];
}

-(void)methodB {
    // got result, do more stuff
}

[classB methodB]
不起作用,但我不知道如何实现,因此非常感谢您的帮助。

在调用方法之前,您必须初始化类的对象以调用显式方法。。。像

#classA.m

-(void)methodA:(classB *)classB {

#classB.m    

[classAInstance methodA:self];
// classA.m
- (void)somethingA{ }
如果该方法是类的隐式方法,则代码可以工作,您不需要创建或初始化类对象,但请记住,隐式方法只能由类调用。。像

// classB.m
+ (void)somthingB {}
我不熟悉objective-c,所以请容忍我

如果你不介意的话,这次我会穿上衣服的

实现所需的一种方法是通过“组合”,这意味着编写A,使其具有一个成员变量,即B的实例。然后A可以使用B的实例调用B中的方法:

A.h:

.
B.h:

===

因为你写了:

[classB methodB];
…也许您想调用B中的类方法

A.h:

B.h:


我认为其他海报忽略了一个非常重要的东西:保持循环。任何试图引用其父对象的子方法都需要使用弱引用或_不安全的u未恢复的修饰符。如果不这样做,则可能会使父对象在保留周期中被捕获。如果我理解这个问题,您只想在类“B”对象中的某个方法完成时调用类“a”对象中的某个方法?我通常采用两种方式之一:委托和协议(较难的概念)或NSNotificationCenter(较难的概念)。在您的例子中,由于您只是试图在另一个类中的另一个方法完成时“通知”一个方法,因此通知中心似乎更易于使用。这里有一个非常好的教程:但这里有一个基本前提:

在完成工作的方法(B类)中,在方法末尾插入如下内容:

NSNotification* notification = [NSNotification notificationWithName:@"MyNotification" object:self];  
[[NSNotificationCenter defaultCenter] postNotification:notification];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"MyNotification" object:nil];
然后,在类A init方法中注册以接收该通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MethodToCallAfterNotification:) name:@"MyNotification" object:nil];
每当B类方法完成时,它将广播一个“MyNotification”通知。您的类A对象正在侦听这些通知,因此每当该通知在您的应用程序中广播时,它将自动调用您指定的选择器

只需确保在类a实现文件中创建dealloc方法,并按如下方式注销观察者:

NSNotification* notification = [NSNotification notificationWithName:@"MyNotification" object:self];  
[[NSNotificationCenter defaultCenter] postNotification:notification];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"MyNotification" object:nil];

如果您有兴趣学习如何使用弱引用的委托方法调用执行此操作,我在此处发布了一个答案:

我尝试了此操作,但在
[my_b methodB]行中出现错误
“例如,接收方类型“B”的消息是一个转发声明”
。您在导入过程中的某个地方丢失了。如果我在a.h中导入B.h,我会得到错误
ld:1架构armv7的重复符号clang:error:linker命令失败,退出代码为1(使用-v查看调用)
你为什么不看看我的导入并复制它们呢?其中包含
@interface
的文件是.h文件;其中包含
@implementation
的文件是.m文件。将其他文件添加到我的帖子中..7stud,也许我没有完全理解这一点,但是你的'B'类不会调用完全不同的'A'方法吗?这不是李nk到创建子“B”对象的原始“A”对象。您编写它的方式不起作用。对于类方法,您需要将classB:methodB中的小“-”更改为“+”。
#import "A.h"
#import "B.h"

@implementation A

- (void)methodA 
{
    [B classMethodB];
}

@end
#import <Cocoa/Cocoa.h>

@interface B : NSObject {

}
+ (void)classMethodB;
- (void)do_stuff;

@end
#import "B.h"
#import "A.h"

@implementation B

- (void)do_stuff
{
    A* a = [[A alloc] init];
    [a methodA];
}


+ (void)classMethodB   //Note the '+'
{
    NSLog(@"hello");
}

@end
NSNotification* notification = [NSNotification notificationWithName:@"MyNotification" object:self];  
[[NSNotificationCenter defaultCenter] postNotification:notification];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MethodToCallAfterNotification:) name:@"MyNotification" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"MyNotification" object:nil];