RE: Communication between objects
RE: Communication between objects
- Subject: RE: Communication between objects
- From: "Jon C. Munson II" <email@hidden>
- Date: Wed, 11 Feb 2009 10:52:34 -0500
- Organization: JTA Enterprises LLC
You need a line of code in your controller object thus:
[modelObject1 setModelObject2:modelObject2];
Then, in your object code, something like:
- (void)setModelObject2:(ModelObject2 *)anObject
{
modelObject2 = anObject;
}
You will need to include the header from ModelObject2 in your ModelObject1
code, and declare an instance variable for ModelObject2 in your ModelObject1
code.
Repeat for the other object.
Something like that anyway.
Peace, Love, and Light,
/s/ Jon C. Munson II
> -----Original Message-----
> From: cocoa-dev-bounces+jmunson=email@hidden [mailto:cocoa-dev-
> bounces+jmunson=email@hidden] On Behalf Of Jason Wiggins
> Sent: Wednesday, February 11, 2009 10:43 AM
> To: Clint Shryock; I. Savant
> Cc: Cocoa-Dev List
> Subject: Re: Communication between objects
>
> Hi Clint (and I.S.)
>
> Thanks for your replies. I understand MVC and am happy with that now
> that a few thoughts have been cleared for me (thanks I.S.). OK, I now
> understand that and object knows nothing about its parent. Cool. I
> thought that was the case, but I wasn't convinced.
>
> Now, I understand I need a pointer to the "other object", but I am
> failing (in a BIG way) in understanding HOW to get it or create it.
> myControllerObject has a pointer to it's children because it knows
> about it because it created it.
> But if modelObject1 doesn't know modelObject2 even exists, how can I
> create a pointer to it? Where do I get this reference from?
>
> Pseudo code...
>
> @interface
> ModelObject1 *modelObject1;
> ModelObject2 *modelObject2;
>
> @implementation
> myControllerObject {
>
> int one, two;
> modelObject1 = [[ModelObject1 alloc] init]; // I now have object 1
> modelObject2 = [[ModelObject2 alloc] init]; // I now have object 2
>
> one = [modelObject1 doMethod]; // doMethod returns an int
> two = [modelObject2 doMethod]; // doMethod returns an int
>
> }
> ....
>
> // The "code" above I understand and works OK.
>
>
> @interface
>
>
> @implementation
> modelObjectA {
> int a;
>
> a = [modelObject2 doMethod];
>
> }
>
> If I do the above, I get "error:'modelObject2' undeclared (first use
> in this function)"
> What do I do in modelObject1's code to make this work? Where do I get
> a reference for the pointer?
>
> Thanks,
>
> Regards,
> Jason
>
>
> On 12/02/2009, at 02:00 , Clint Shryock wrote:
>
> > in order for modelObject1 to talk to modelObject2, it must have a
> > reference to that object, similar to how myControllerObject has a
> > reference to both modelObject1 and modelObject2 which you use to
> > send messages.
> >
> > +Clint
> >
> > On Wed, Feb 11, 2009 at 8:42 AM, Jason Wiggins <email@hidden
> > > wrote:
> > Hi all,
> >
> > Can someone please help me with this, I'm tearing my hair out!
> >
> > Say I have myControllerObject. I alloc and init modelObject1 and
> > also modelObject2.
> > How do I access the ivars of modelObject1 from modelObject2 and vice
> > versa?
> >
> > modelObject1 and modelObject 2 both exist somewhere in memory, so
> > how do I get a pointer to or make a connection between these two
> > object?
> >
> > Also, how do I access the ivars of myControllerObject from either
> > modelObject1 or modelObject2?
> >
> > Is it possible to find out from myObject1 or myObject2 who init and
> > alloc them?
> >
> > From what I've read to far (Objective C-2.0 Programming Language and
> > Cocoa Fundamentals Guide) I've been unable to find the answer to
> > these questions.
> > I can send messages from myControllerObject to myObject1 or
> > myObject2 and get returned information no worries, that I do get.
> >
> > Thanks,
> >
> > Regards,
> > Jason
> > _______________________________________________
> >
> > Cocoa-dev mailing list (email@hidden)
> >
> > Please do not post admin requests or moderator comments to the list.
> > Contact the moderators at cocoa-dev-admins(at)lists.apple.com
> >
> > Help/Unsubscribe/Update your Subscription:
> >
> > This email sent to email@hidden
> >
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden