0

I have a "class-A" which contains a method

-(void)methodA
{
//Logic
}

I have another "Class-B" which is a method

-(void)methodB
{
//Logic
}

Now i am trying to call methodA from Class B

So what i do

In Class B

Create an object of "Class-A"

ClassA *a;

@property(nonatomic,retain)ClassA *a;

@synthesize a;

-(void)methodB
{
[self.a methodA];
}

But the method is not called. So what am i doing wrong or any other approach for doing this ?

user930195
  • 432
  • 1
  • 5
  • 19
  • 4
    You created a _pointer_ to ClassA in ClassB, but did you set it to point to an actual object of type ClassA? – Joachim Isaksson Mar 12 '12 at 12:35
  • check answers here http://stackoverflow.com/questions/9629417/calling-a-method-from-another-class-in-objective-c/9629709#comment12245597_9629709 – janusfidel Mar 12 '12 at 12:37
  • you need to allocate the class to send messages to it, maybe you did it, but if this is all the code you're using, you didn't. – Simon Mar 12 '12 at 12:43

1 Answers1

1
//In class A
//classA.h

@interface classA : NSObject
  -(void)methodA;
@end

//classA.m
@implementation classA
-(void)methodA
{
    //Logic
}
@end


//In class B
//classB.h

#import classA.h 
@interface classB : NSObject

@property(nonatomic,retain)classA *a;

@end

//classB.m
@implementation classB

@synthesize a;

-(void)methodB
{
    if(!self.a) self.a = [[classA alloc]init];
    [self.a methodA];
    //Logic
}

@end
Krrish
  • 2,256
  • 18
  • 21