Re: is protected broken, or am I?
Re: is protected broken, or am I?
- Subject: Re: is protected broken, or am I?
- From: Cameron Hayne <email@hidden>
- Date: Fri, 11 Oct 2002 00:49:25 -0400
On 10/10/02 6:42 pm, "matt neuburg" <email@hidden> wrote:
>
MyClass<----------->window
>
(File's Owner)
>
>
There is a bunch of code in MyClass, and there are a lot of connections set up
>
between the MyClass instance and the window in the nib (represented by the
>
double-headed arrow). Now I have a bunch of *other* code that governs this
>
*other* behavior of the *same* window. The question is, where shall I put this
>
code?
>
I don't want to put it into MyClass because MyClass is already huge, and also
>
because this will mean that just about every method in MyClass will be a huge
>
if/then statement doing one thing on one set of occasions and another thing on
>
another set of occasions.
>
That is just the sort of the problem that classes are supposed to solve.
>
Instead of if/then, you just supply a different class, and the right thing
>
happens because whichever class the message is sent to an instance of, it
>
contains the right code.
>
But how can I slot that code into the above picture? Basically I want that
>
code to be where MyClass's code is in the picture - with access to all those
>
connections to the window.
You talk about "the *other* behavior of the *same* window". But it isn't the
same window - it looks differently.
I think I understand what you want, but it seems to me you are asking about
two different things at the same time. The first question is an Interface
Builder question - Question#1 is "Suppose I want two windows which are very
similar - how can I make the second window be like the first one but with
some added stuff?". (And of course you want the second window to track any
later modifications you make to the first window - so copy & paste is not
the answer!) I don't know the answer to this question. I assume you should
be able to do it in Interface Builder by creating a Custom View holding your
widgets and then subclassing that Custom View to get another custom view
which you could add the extra stuff to, but the details of how to do this
elude me.
The second question is an easier one (Question#2): "If I have two windows
which are very similar, how can I create a controller class for the second
one which inherits all the code in the first controller class and allows me
to add new code for the extra stuff?". This one I know the answer to (it is
what I thought you were asking about in the original query) - you make the
second controller class a subclass of the first one.
For example, suppose your first window (WindowA) has one label (label1) and
one button (button1). You create outlets with those names in your first
controller class (call it MyClass) and the .h and .m files might look like
this:
---------------------------------------------------------------
/* MyClass.h */
#import <Cocoa/Cocoa.h>
@interface MyClass : NSObject
{
IBOutlet id button1;
IBOutlet id label1;
}
- (IBAction)button1Action:(id)sender;
@end
/* MyClass.m */
#import "MyClass.h"
@implementation MyClass
- (IBAction)button1Action:(id)sender
{
[label1 setStringValue:@"Hello"];
}
@end
---------------------------------------------------------------
You create an instance of WindowA and an instance of MyClass and you hook up
the outlets and action.
Now suppose your second window (WindowB), is just like WindowA, but with one
additional button (button2). Not having an answer to Question#1 above, you
create this window by copy & paste from WindowA and then add the second
button. You then subclass MyClass to create MySubclass and add an outlet
'button2' and an action 'button2Action'. The .h and .m files might look like
this:
---------------------------------------------------------------
/* MySubclass.h */
#import <Cocoa/Cocoa.h>
#import <MyClass.h>
@interface MySubclass : MyClass
{
IBOutlet id button2;
}
- (IBAction)button2Action:(id)sender;
@end
/* MySubclass.m */
#import "MySubclass.h"
@implementation MySubclass
- (IBAction)button2Action:(id)sender
{
[label1 setStringValue:@"World!"];
}
@end
---------------------------------------------------------------
You create an instance of MySubclass and hook up the outlets (label1,
button1, button2) and actions. WindowA and WindowB are sharing the code of
MyClass.
Does anyone have a good answer to Question#1 ?
--
Cameron Hayne (email@hidden)
Hayne of Tintagel
_______________________________________________
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.