Calling a method from another class in Objective C

2019-01-11 15:29发布

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.

5条回答
beautiful°
2楼-- · 2019-01-11 16:05

I'm pretty sure what you are looking for is blocks.

In class A, you can define calculate() as a block.

Then in class B, you could have some kind of method that took in a block for calculation and returned some new instance of B based on the calculation (I'm assuming that is what was intended?).

查看更多
聊天终结者
3楼-- · 2019-01-11 16:07

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
查看更多
别忘想泡老子
4楼-- · 2019-01-11 16:12

You could make one class a delegate of the other but that is kind of arbitrary relationship-wise.

Is there any reason not to use inheritance - both A + B subclass from Z - and Z is where your common method is? Then they could both call [self calculate];

查看更多
来,给爷笑一个
5楼-- · 2019-01-11 16:12

you can do this using notificationCenter. e.g

Class A .h

@interface A: UIViewController
-(void)classAandBMethod;
@end

.m

@implementation
-(void)willPushToClassB
{
 [[NSNotificationCenter defaultCenter] removeObserver:self];
 [[NSNotificationCenter defaulCenter] addObserver:self selector:@selector(classAandBMethod) name:@"classAandBMethod_name" object:nil];
 //push class B
 ClassB *b = [ClassB alloc] initWithNibName:@"ClassB" ........];
 [self.navigationController pushViewController:b animated:YES];
}
@end

when you call the method in ClassB:

//this method in classB will call the method in Class a.
-(void)callMethodInClassA{
 NSNotification *subject = [NSNotification defaultCenter];
 [subject postNotificationName:@"classAandBMethod_name" object:nil userinfo:whatEverInfoYouWant_dict];
 [subject removeObserver:self];
}
查看更多
劳资没心,怎么记你
6楼-- · 2019-01-11 16:30

What you are looking is possible with Objective C.

You can refer this post. But for that you have to learn some syntax of Objective C.

If you are not familiar with Objective C & not direct deal with Cocoa frame work then you can do your work using Objective C++. Where you can write your code in C++.

Here you can use function pointer & pass static method of class A.

Or you can define interface class. Derive class A from that & pass object of A to class B.

查看更多
登录 后发表回答