Subclassing question

  • I want to subclass NSView so that it has a setVisible: method. Say I call it
    HidingView. The details of doing that are no problem, but how do I go about
    making things that are already subclasses of NSView (e.g.
    NSProgressIndicator, NSImageView, etc) inherit from this subclass rather
    than NSView? Sure I could subclass those subclasses every time and take care
    of things there, but it sure would be convenient if I could just have all
    subclasses of NSView inherit from HidingView so I could hide things when I
    need to. Categories are almost perfect except...no instance variables. What
    am I missing?

    PS here's the simple subclass

    - (void)setVisible:(BOOL)b
    {
        if (!b)
        {
            if (![self isVisible])
                return;
            savedFrame = [self frame];
            [self setFrame:zeroFrame];
            [self setNeedsDisplay:YES];
        }
        else
        {
            if ([self isVisible])
                return;
            [self setFrame:savedFrame];
            [self setNeedsDisplay:YES];
        }
    }

    - (BOOL)isVisible
    {
        NSRect visRect = [self visibleRect];

        return (visRect.size.width != 0);
    }
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • To answer my own question (I hate it when I do that), using a category with
    a static NSMutableDictionary seems to work. Although I can't figure out
    where the best place to remove the items from the dictionary when the object
    goes away would be since overriding dealloc in a category is sort of a bad
    idea.

    The below is working pretty well. Now I can finally hide an unhide all my
    views with ease! One of the two things I miss about Carbon...

    NSRect                    zeroFrame = {{0,0},{0,20}};// a true zero frame
    confuses 10.1
    NSMutableDictionary        *savedFrames = nil;//since a category can't have
    instance variables, keep them in this dictionary

    @implementation NSView (HidingView)

    - (void)setVisible:(BOOL)b
    {
        if (!savedFrames)
            savedFrames = [[NSMutableDictionary dictionaryWithCapacity:20]
    retain];
        if (![savedFrames objectForKey:[NSString stringWithFormat:@"%p", self]])
            [savedFrames setObject:[NSStringFromRect([self frame]) retain]
    forKey:[NSString stringWithFormat:@"%p", self]];

        if (!b)
        {
            if (![self isVisible])
                return;
            [savedFrames setObject:NSStringFromRect([self frame])
    forKey:[NSString stringWithFormat:@"%p", self]];
            [self setFrame:zeroFrame];
            [self setNeedsDisplay:YES];
        }
        else
        {
            if ([self isVisible])
                return;
            [self setFrame:NSRectFromString([savedFrames objectForKey:[NSString
    stringWithFormat:@"%p", self]])];
            [self setNeedsDisplay:YES];
        }
    }

    - (BOOL)isVisible
    {
        NSRect visRect = [self visibleRect];

        return (visRect.size.width != 0);
    }
    @end

    On 1/22/03 10:28 PM, "John Scalo" <scalo...> wrote:

    > I want to subclass NSView so that it has a setVisible: method. Say I call it
    > HidingView. The details of doing that are no problem, but how do I go about
    > making things that are already subclasses of NSView (e.g.
    > NSProgressIndicator, NSImageView, etc) inherit from this subclass rather
    > than NSView? Sure I could subclass those subclasses every time and take care
    > of things there, but it sure would be convenient if I could just have all
    > subclasses of NSView inherit from HidingView so I could hide things when I
    > need to. Categories are almost perfect except...no instance variables. What
    > am I missing?
    >
    > PS here's the simple subclass
    >
    > - (void)setVisible:(BOOL)b
    > {
    > if (!b)
    > {
    > if (![self isVisible])
    > return;
    > savedFrame = [self frame];
    > [self setFrame:zeroFrame];
    > [self setNeedsDisplay:YES];
    > }
    > else
    > {
    > if ([self isVisible])
    > return;
    > [self setFrame:savedFrame];
    > [self setNeedsDisplay:YES];
    > }
    > }
    >
    > - (BOOL)isVisible
    > {
    > NSRect visRect = [self visibleRect];
    >
    > return (visRect.size.width != 0);
    > }
    > _______________________________________________
    > cocoa-dev mailing list | <cocoa-dev...>
    > Help/Unsubscribe/Archives:
    > http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    > Do not post admin requests to the list. They will be ignored.
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
previous month january 2003 next month
MTWTFSS
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    
Go to today