• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Subclassing question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Subclassing question


  • Subject: Re: Subclassing question
  • From: John Scalo <email@hidden>
  • Date: Wed, 22 Jan 2003 23:55:12 -0800

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" <email@hidden> 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 | 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.
_______________________________________________
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.

References: 
 >Subclassing question (From: John Scalo <email@hidden>)

  • Prev by Date: NSButtonCell & tableView (s)
  • Next by Date: Re: localized strings from another bundle?
  • Previous by thread: Subclassing question
  • Next by thread: NSButtonCell & tableView (s)
  • Index(es):
    • Date
    • Thread