Calling a method from another class in Objective C

后端 未结 5 970
鱼传尺愫
鱼传尺愫 2020-12-03 09:28

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 me

相关标签:
5条回答
  • 2020-12-03 09:43

    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];

    0 讨论(0)
  • 2020-12-03 09:43

    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];
    }
    
    0 讨论(0)
  • 2020-12-03 09:48

    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?).

    0 讨论(0)
  • 2020-12-03 09:49

    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.

    0 讨论(0)
  • 2020-12-03 09:55

    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
    
    0 讨论(0)
提交回复
热议问题