-3

I have written two classes which contains same method (print). I want to access first class print method using second class object. How i can achieve this?

Code:

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

@implementation classA

-(void) print
{
    NSLog(@"hello");
}

@end

@interface classB : classA

-(void) print;
@end

@implementation classB

-(void) print{
    NSLog(@"hey");
}
@end

Now i created second class object like

classB *B = [classB alloc]init];
Coder
  • 1,661
  • 4
  • 27
  • 50

2 Answers2

2

use delegates to access other classes @protocol

NANNAV
  • 4,875
  • 4
  • 32
  • 50
  • How can we achieve this using @protocol? I am new to objective c... please suggest in this regard. – Coder Nov 06 '12 at 05:52
  • google "objective c delegate pattern" and there's thousands of results with good examples. Here's the first: http://enroyed.com/ios/delegation-pattern-in-objective-c-and-writing-custom-delegates/ – bennythemink Nov 06 '12 at 06:36
1

you can do like this way also

@implementation view1
(void)someMethod
{
   ......code of method...
}

@implementation view2
(void)fistMethod
{
    view1 *abc = [[view1 alloc]init];
    [abc someMethod];
    [abc release];
}

also check this Objective-C call function on another class?

Community
  • 1
  • 1
Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144