【问题标题】:Calling a method from another class in Objective C在 Objective C 中调用另一个类的方法
【发布时间】:2012-03-09 05:35:01
【问题描述】:

我有 2 个班级,比如 A 班和 B 班。 B 类是在 A 类中创建的。 我在A类中有一个方法,需要在A类和B类中都执行。在A类中调用该方法本身就可以了。但我不确定在 B 类中调用该方法。 我尝试将方法声明为静态,但由于我不能在静态方法中使用实例变量,我认为使用委托是个好主意。由于我来自 C# 背景,我不确定在 Objective C 中使用它。从概念上讲,我已经在 C# 中实现了我需要的东西,如下所示。只是想知道它在 Objective C 中的等价物。

class A
{

    public A()
    {
        B myclass = new B(() => calculate());                
    }

    public void calculate()
    {
        // todo
    }
}

class B
{
    public B(Action calculate)
    {
        calculate();
    }
}

是否可以使用协议来做到这一点。

【问题讨论】:

    标签: iphone objective-c ios delegates


    【解决方案1】:

    我只是在研究时偶然看到了这篇文章。这是一个示例代码:

    ClassA.h 文件:

    #import <Foundation/Foundation.h>
    #import "ClassB.h"
    
    @interface ClassA : NSObject <ClassBDelegate>
    @end
    

    ClassA.m 文件:

    #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 文件:

    #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 文件:

    #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
    

    【讨论】:

      【解决方案2】:

      Objective C 可以实现您所期待的。

      您可以参考this post。但为此,您必须学习一些 Objective C 的语法。

      如果您不熟悉 Objective C 并且不直接处理 Cocoa 框架,那么您可以使用 Objective C++ 来完成您的工作。您可以在哪里用 C++ 编写代码。

      这里可以使用函数指针&传递A类的静态方法。

      或者你可以定义接口类。从该类派生 A 类并将 A 的对象传递给 B 类。

      【讨论】:

        【解决方案3】:

        你可以让一个类成为另一个类的代表,但这是一种任意的关系方式。

        是否有任何理由不使用继承 - Z 的 A + B 子类 - 并且 Z 是您的常用方法所在的位置?然后他们都可以拨打[self calculate];

        【讨论】:

        • 我更喜欢使用委托。想看看在上述情况下它是如何完成的。
        【解决方案4】:

        您可以使用通知中心来执行此操作。例如

        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
        

        当你调用 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];
        }
        

        【讨论】:

        • 这对我来说绝对有效。但我想,这不是我想要的方式。
        • 编辑:在 ClassB 中将 (void)callMethodInClassA 中的 NSNotification 更改为 NSNotificationCenter
        【解决方案5】:

        我很确定你要找的是块。

        在 A 类中,您可以将 calculate() 定义为一个块。

        然后在 B 类中,您可以有某种方法,该方法接受一个块进行计算,并根据计算返回一些 B 的新实例(我假设这是预期的结果?)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-15
          • 1970-01-01
          • 1970-01-01
          • 2015-08-01
          • 2015-07-10
          • 2013-03-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多