-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];
4
  • 1
    possible duplicate of stackoverflow.com/questions/5063484/… Commented Nov 6, 2012 at 5:54
  • I got the answer from this stack overflow link. Thanks ilight. Commented Nov 6, 2012 at 6:03
  • Also you should capitalise your class names i.e. ClassB Commented Nov 6, 2012 at 6:35
  • 1
    Try delegated functionality to call the method Commented Nov 8, 2012 at 5:47

2 Answers 2

2

use delegates to access other classes @protocol

Sign up to request clarification or add additional context in comments.

2 Comments

How can we achieve this using @protocol? I am new to objective c... please suggest in this regard.
google "objective c delegate pattern" and there's thousands of results with good examples. Here's the first: enroyed.com/ios/…
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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.