在Objective C中调用另一个类的方法 [英] Calling a method from another class in Objective C
问题描述
类A在类A中创建。
我有一个类A中的方法,需要在类A和类A中执行在A类本身调用该方法很好。但我不知道在类B中调用该方法。
我已经尝试将该方法声明为静态,但是由于我不能在静态方法中使用实例变量,我认为使用代理将是一个好主意。由于我来自C#背景,我不确定在Objective C中使用它。从概念上讲,我已经在C#中实现了我需要的,如下所示。只想知道目标C中的等价物。
class A
{
public A()
{
B myclass = new B(()=> calculate());
}
public void calculate()
{
// todo
}
}
class B
{
public B(Action calculate)
{
calculate();
}
}
可以使用协议来执行此操作。 p>
我刚刚碰巧在研究时看到这篇文章。以下是一个示例代码:
ClassA.h文件:
#import< Foundation / Foundation.h>
#importClassB.h
@interface ClassA:NSObject&ClassBDelegate>
@end
ClassA.m文件:
#importClassA.h
@implementation ClassA
- (void)createAnInstanceOfClassB
{
ClassB * myClassB = [[ClassB alloc] init]; //创建一个ClassB的实例
myClassB.delegate = self; //将self设为委托
// [myClassB optionalClassBMethod]; //这是您的问题的可选项。如果你还要ClassA从ClassB
- (void)计算
{
NSLog(@Do calculate thing!); //计算可以从ClassB或ClassA
@end
ClassB.h文件:
#import< Foundation / Foundation.h>
@protocol ClassBDelegate< NSObject>
- (void)calculate; //方法从ClassA
@end
@interface ClassB:NSObject
@property(assign)id&ClassBDelegate>代表;
// - (void)optionalClassBMethod; //这是你的问题的选择。如果您也希望ClassA从ClassB调用方法
@end
ClassB.m文件:
#importClassB.h
@implementation ClassB
@synthesize delegate;
- (void)whateverMethod
{
[self.delegate calculate]; //调用方法计算在ClassA
}
// - (void)optionalClassBMethod //这是您的问题的可选项。如果您也希望ClassA从ClassB
// {
// NSLog(@optionalClassBMethod)调用方法;
// [self whateverMethod];
//}
@end
I have 2 classes, say class A and class B. Class B is created in class A. I have a method in class A, which needs to be executed in both class A and class B. Calling the method in class A itself is fine. But I am not sure about calling the method in class B. I have tried declaring the method as static, but since I can't use instance variables inside the static method, I think using delegates would be a good idea. Since I am from a C# background, I am not sure about using it in Objective C. Conceptually, I have implemented what I need in C# as shown below. Just wanted to know what the equivalent of it would be in Objective C.
class A
{
public A()
{
B myclass = new B(() => calculate());
}
public void calculate()
{
// todo
}
}
class B
{
public B(Action calculate)
{
calculate();
}
}
Is it possible to do this using protocols.
I just happened to see this post while researching. Here is a sample code:
ClassA.h file:
#import <Foundation/Foundation.h>
#import "ClassB.h"
@interface ClassA : NSObject <ClassBDelegate>
@end
ClassA.m file:
#import "ClassA.h"
@implementation ClassA
-(void)createAnInstanceOfClassB
{
ClassB *myClassB = [[ClassB alloc]init]; //create an instance of ClassB
myClassB.delegate = self; //set self as the delegate
// [myClassB optionalClassBMethod]; //this is optional to your question. If you also want ClassA to call method from ClassB
}
-(void)calculate
{
NSLog(@"Do calculate thing!"); // calculate can be called from ClassB or ClassA
}
@end
ClassB.h file:
#import <Foundation/Foundation.h>
@protocol ClassBDelegate <NSObject>
-(void)calculate; //method from ClassA
@end
@interface ClassB : NSObject
@property (assign) id <ClassBDelegate> delegate;
//-(void)optionalClassBMethod; //this is optional to your question. If you also want ClassA to call method from ClassB
@end
ClassB.m file:
#import "ClassB.h"
@implementation ClassB
@synthesize delegate;
-(void)whateverMethod
{
[self.delegate calculate]; // calling method "calculate" on ClassA
}
//-(void)optionalClassBMethod //this is optional to your question. If you also want ClassA to call method from ClassB
//{
// NSLog(@"optionalClassBMethod");
// [self whateverMethod];
//}
@end
这篇关于在Objective C中调用另一个类的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!