views:

60

answers:

2

I have class1.m . I declared a method and written in it. Now i need to call it into another class. How can I make it? Can we use extern for it like we use for variables. Thank you.

+3  A: 

You should separate your declaration and definition, and place the declaration for class1 in class1.h. Then, you should include class1.h using #import "class1.h" in your source file for class2. Within class2, you can instantiate and use class1 as follows:

class1* instance_of_class1 = [[class1 alloc] init];
[class1 invokeMyMethod];

When you are done using your instance, be sure to decrement the reference count via release as in:

[instance_of_class1 release];
instance_of_class1 = nil;
Michael Aaron Safyan
Thank You.I had a method (method1) in in Class1. I declared it in Class1.h and implemented in Class1.m. I have Class2 and it have a selector:@selector(method1). In this case how can I do it ?
srikanth rongali
It is more efficient to simply call the method directly using [instance method1]. However, if you need to use dynamic typing and invoke the selector in that way, you can use the "performSelector" method which is defined in NSObject (which should be a base class, either directly, or indirectly of your class).
Michael Aaron Safyan
+1  A: 

I highly recommend reading Apple's Objective-C Programming Guide which will cover the fundamentals you need to know.

Preston