Re: Executing methods in one class from within another
Re: Executing methods in one class from within another
- Subject: Re: Executing methods in one class from within another
- From: Robert Goldsmith <email@hidden>
- Date: Wed, 12 Jun 2002 10:05:06 +0100
>
I have some app, document based or not is not of concern, I
>
think. In my app I have two NSObject subclasses, myClassA,
>
myClassB. myClassA has an accessor, let's say for some NSString
>
-(NSString *)myString. Now, if I need to retrieve the value that
>
is currently in this string, but need to do so in myClassB, I
>
don't know how to go about this, if instances of myClassA and
>
myClassB were created at the same level, so that I could not say
>
#import myClassA in myClassB and #import myClassB in myClassA,
>
which, of course, gives me a compiler error. I can either do the
>
one or the other, but not both. myClassA doesn't, at this stage,
>
'possess' an instance of myClassB, and neither vice versa, so I
>
have problems having the two accessing values from each other if
>
I do not have some central switchboard widget that allows me to
>
get hold of one from within the other.
>
There are a number of ways to allow two classes that are not
related to talk to each other but the first question you have to
ask is "if they should not be related, why does classB need info
from classA?". The easiest way usually is to think hard about the
relationship between the two classes and, as has been suggested,
either create one of the classes inside the other as an instance
variable (don't try to do this to both of them or things will get
messy even if the @class directive may allow it...) or create
both of the classes as instances of a parent.
However, assuming that you have already been thinking about this
after reading the other posts, I will run through some of the
alternatives. First, if you are trying to keep a separation
between distinct parts of a program (e.g. a hardware driver and
the interface to control it) then notifications can be useful.
Notifications can also be used when writing threaded or
asynchronous code where you want to be able to do two things at
once. However, for simple communication of two classes this is
rather overkill. The other easy option is to use your Nib file
(if you have one) as a parent to both your classes. It is often
the case that you will have a number of classes acting to help
control an interface and these classes, although they are
separate, not really related and look after different parts of
the interface, they need to talk to each other. To cope with
this, you can use Interface Builder.
If you create instances of both classes in your Nib file
(remember, this should only be done when dealing with classes
helping with an interface really) then, in each class, you can
create an IBOutlet for the other class and link them in Interface
Builder. Then, to send a message from classA to classB, you
simply send a message in classA to the IBOutlet named in the
classA header file. When the Nib is loaded, all the linking to
make this work correctly is done automatically :)
If neither of these seem appropriate, give more detail about what
the two classes do and how they might be related and we can try
to find ways to restructure your classes so it all works :)
Hope this helps
Robert Goldsmith
---
GnuPG public key:
http://www.Far-Blue.co.uk/RSGoldsmith.asc
[demime 0.98b removed an attachment of type application/pgp-signature which had a name of PGP.sig; charset=US-ASCII]
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.