<i id='mIJl6'><tr id='mIJl6'><dt id='mIJl6'><q id='mIJl6'><span id='mIJl6'><b id='mIJl6'><form id='mIJl6'><ins id='mIJl6'></ins><ul id='mIJl6'></ul><sub id='mIJl6'></sub></form><legend id='mIJl6'></legend><bdo id='mIJl6'><pre id='mIJl6'><center id='mIJl6'></center></pre></bdo></b><th id='mIJl6'></th></span></q></dt></tr></i><div id='mIJl6'><tfoot id='mIJl6'></tfoot><dl id='mIJl6'><fieldset id='mIJl6'></fieldset></dl></div>

          <bdo id='mIJl6'></bdo><ul id='mIJl6'></ul>
        <legend id='mIJl6'><style id='mIJl6'><dir id='mIJl6'><q id='mIJl6'></q></dir></style></legend>

        <small id='mIJl6'></small><noframes id='mIJl6'>

        <tfoot id='mIJl6'></tfoot>

        在 Objective C 中调用另一个类的方法

        时间:2023-07-28
          <bdo id='8hl76'></bdo><ul id='8hl76'></ul>

            <tbody id='8hl76'></tbody>
          • <legend id='8hl76'><style id='8hl76'><dir id='8hl76'><q id='8hl76'></q></dir></style></legend>

            <small id='8hl76'></small><noframes id='8hl76'>

              1. <i id='8hl76'><tr id='8hl76'><dt id='8hl76'><q id='8hl76'><span id='8hl76'><b id='8hl76'><form id='8hl76'><ins id='8hl76'></ins><ul id='8hl76'></ul><sub id='8hl76'></sub></form><legend id='8hl76'></legend><bdo id='8hl76'><pre id='8hl76'><center id='8hl76'></center></pre></bdo></b><th id='8hl76'></th></span></q></dt></tr></i><div id='8hl76'><tfoot id='8hl76'></tfoot><dl id='8hl76'><fieldset id='8hl76'></fieldset></dl></div>
                1. <tfoot id='8hl76'></tfoot>
                  本文介绍了在 Objective C 中调用另一个类的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

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

                  ClassA.h file:

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

                  ClassA.m 文件:

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

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

                  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];
                  /
                  • <bdo id='DgHye'></bdo><ul id='DgHye'></ul>

                        <legend id='DgHye'><style id='DgHye'><dir id='DgHye'><q id='DgHye'></q></dir></style></legend>

                      1. <small id='DgHye'></small><noframes id='DgHye'>

                        <tfoot id='DgHye'></tfoot>
                      2. <i id='DgHye'><tr id='DgHye'><dt id='DgHye'><q id='DgHye'><span id='DgHye'><b id='DgHye'><form id='DgHye'><ins id='DgHye'></ins><ul id='DgHye'></ul><sub id='DgHye'></sub></form><legend id='DgHye'></legend><bdo id='DgHye'><pre id='DgHye'><center id='DgHye'></center></pre></bdo></b><th id='DgHye'></th></span></q></dt></tr></i><div id='DgHye'><tfoot id='DgHye'></tfoot><dl id='DgHye'><fieldset id='DgHye'></fieldset></dl></div>
                            <tbody id='DgHye'></tbody>