代码之家  ›  专栏  ›  技术社区  ›  pankaj

从另一个类的方法调用当前类的方法

  •  -1
  • pankaj  · 技术社区  · 14 年前

    我正在研究一个类A,它有一个方法-(void)dosmthing1。我打电话给 类B中的另一个方法-(void)dosmthing2。现在,在类B中执行一些操作后,该方法应该回调上一个类的方法-(void)dosmthing3。

    如何从另一个类调用当前类的方法?有人能帮我吗?

    提前谢谢

    Eddi1:: 我的代码: 甲类

    {
    -(void) MethodA {
    
    }
    
    -(void) MethodB {
       ClassB *clsB = [[ClassB alloc] init];
       [clsB MethodC];
    }  
    }  
    

    乙类

    {
      -(void)MethodC:(selector) {
      //here i want to call MethodA of classA, and i will prefer if it is possible by sending the name of the method as selector in this method(methodC)
    
        }  
    }
    

    EdT2::

    另一个例子是我想做如下的事情:

    ClassB *b = [[ClassB alloc] nitWithTarget:self selector:@selector(methodfromClassA) object:nil];
    

    在这里,我想在类B中的某个任务完成后调用类A的方法,这也是从类A中调用的方法。

    我希望现在很清楚。

    EdTe3:

    - (void)loadView {
        AsyncConnection *async =[[AsyncConnection alloc] init];
        [async getAsync:self callback:@selector(test1)];
    }
    

    以上代码来自头等舱

    -(void)getAsync:(id)anObject callback:(SEL)selector {
        NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:anObject 
                                                                                selector:@selector(selector) 
                                                                                  object:nil];
        [queue addOperation:operation];
        [operation release];
    
    }
    

    以上代码来自二级。这里我想调用一个作为选择器传递的第一类方法。

    4 回复  |  直到 14 年前
        1
  •  2
  •   bbum    14 年前
    - (void)loadView {
        AsyncConnection *async =[[AsyncConnection alloc] init];
        [async getAsync:self callback:@selector(test1)];
    }
    

    -(void)getAsync:(id)anObject callback:(SEL)selector {
        NSInvocationOperation *operation = [[NSInvocationOperation alloc] 
             initWithTarget:anObject 
                   selector:@selector(selector)
                     object:nil];
    
        [queue addOperation:operation];
        [operation release];
    
    }
    

    @selector() @selector(selector) SEL selector

    AsyncConnection loadView

    @property(retain) ClassA *callbackHandler;
    

    - (void)loadView {
        AsyncConnection *async =[[AsyncConnection alloc] init];
        [async setCallbackHandler: self];
    }
    

    self.callbackHandler [self callbackHandler]

        3
  •  1
  •   bbum    14 年前

    classA *aObj;
    
    @property(nonatomic, retain) classA *aObj;
    

    @synthetize aObj;
    

    // after init class b obj
    
    [bOjb setAObj:self];
    

    [aObj whateverMethodOfClassA];
    

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(classA-method-name:) name:@"notification-name" object:class-B-object ];
    

    [[NSNotificationCenter defaultCenter] postNotificationName:@"notification-name" object:self];
    

        4
  •  0
  •   hotpaw2    14 年前