Zaraki
Zaraki

Reputation: 3740

How can i pass object as parameter from a class to another class?

i have three classes.. say one two three.. now i have an object of class one.. i want to pass that object from class two to class three as a parameter by calling a method which is in class three.

what all steps to be taken and kindly explain with examples ..

Upvotes: 1

Views: 3845

Answers (1)

Raphael
Raphael

Reputation: 8192

Is this what you're after?

In your Class2 header declare a method that returns a Class1 pointer.

- (Class1*)objectOfClass1;

Implement Class2,

- (Class1*)objectOfClass1 {return [[Class1 alloc] autorelease];}

In your Class3 header declare a method that accepts an argument of pointer to Class1:

-(void) doSomething:(Class1 *)obj;

Class3 source, implement your logic:

-(void) doSomething:(Class1 *)obj {
    // Use your Class1 object here.
}

And you would call it on Class2 like this:

Class1 *obj1; //Object of class one
Class2 *obj2 = [[Class2 alloc] autorelease]; //object of class two
Class3 *obj3 = [[Class3 alloc] autorelease]; //object of class three

obj1 = [obj2 objectOfClass1]; //retrieve object of class one from object of class two 

[obj3 doSomething:obj1]; //pass object of class one into object of class three

Anyway, I recommend that you take a look at this simple tutorial: Learning Objective-C: A Primer

Upvotes: 1

Related Questions