Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8042543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:37:01+00:00 2026-06-05T04:37:01+00:00

I have 2 classes, say class A and class B. Class B is created

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T04:37:04+00:00Added an answer on June 5, 2026 at 4:37 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Newbie question. I have two c# classes - a code class (say, CodeClass) and
Let's say i have 2 classes: class Class1 { public: std::vector<CustomClass3*> mVec; public: Class1();
Let's say I have some classes like this: abstract class View(val writer: XMLStreamWriter) {
Say you have two classes, order and customer: public class Customer{ public int CustomerId
Say I have two classes, and neither of them are GUI components. Class A
Let's say we have these two classes: public class Base { public static int
Say I have two separate classes, A and B. I also have Repository class
Lets say i have the following c# classes: abstract class a { protected abstract
I have one abstract class -let's say myBase. And I want all the classes
Let's say I have a bunch of classes that look something like... class Foo{

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.