• 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: setHidden: on MacOS 10.2
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: setHidden: on MacOS 10.2


  • Subject: Re: setHidden: on MacOS 10.2
  • From: m <email@hidden>
  • Date: Tue, 4 May 2004 14:58:19 -0700
  • Resent-date: Tue, 4 May 2004 15:57:36 -0700
  • Resent-from: m <email@hidden>
  • Resent-message-id: <email@hidden>
  • Resent-to: Cocoa Dev <email@hidden>

On May 4, 2004, at 2:04 PM, Michael Becker wrote:

Am 04.05.2004 um 17:31 schrieb Glenn Andreas:
This is the way to go fo me. Unfortunately it doesn't seem like I could simply create a category of NSButton with a setHidden: method, right? As far as I understand categories, I cannot create further object variables, and so I cannot store my superview, right?

No, you can't add a variable in a category, but you can fake it.

Basically, you've got a parallel dictionary that maps between the object and the "pseudo-instance var", using the object as the key (note that you need to be careful about reference counting - removing the object from the dictionary when you try to make it go away).

I don't get it. If I cannot add variables in a category, how can I use a dictionary in a category? That dictionary would have to be a variable, too, right?

Use a static variable local to the category.

The thing is: I would like to go down the route which involves the least work. If I choose to subclass, I'd have to subclass NSButton, NSView, NSBox and NSTextField.

Since NSButton, NSBox,etc are all NSView subclasses, make the category a category for NSView and you're done.

Below is an imperfect example of the kind of thing Glen was suggesting. It uses arrays instead of dictionaries, and I think dictionaries is actually the better approach. Note also Glen's admonition that you have to be careful about reference counting (left as an exercise for you, the code below will retain views long after you're done with them).

In your code, you'd replace calls to the Mac OX 10.3 only NSView method "setHidden" with calls to this category's "makeHidden" method.

static NSMutableArray* sSuperviews = nil;
static NSMutableArray* sSubviews = nil;

@implementation NSView ( FSViewAdditions )

// --------------------
// makeHidden
// --------------------
- (void) makeHidden:(BOOL)inHide
{
long systemVersion = getSystemVersion();

if (systemVersion >= 0x1030)
{
[self setHidden:inHide];
}
else
{
if (!sSuperviews)
{
sSuperviews = [[NSMutableArray arrayWithCapacity:64] retain];
}

if (!sSubviews)
{
sSubviews = [[NSMutableArray arrayWithCapacity:64] retain];
}

NSMutableArray* superviews = sSuperviews;
NSMutableArray* subviews = sSubviews;

NSView* superview = [self superview];

if (inHide && superview)
{
[superviews addObject: superview];
[subviews addObject: self];

[self removeFromSuperview];
}
else if(!inHide && superview == nil)
{
superview = [superviews objectAtIndex:[subviews indexOfObject:self]];

[superview addSubview:self];

NSRect frame = [self frame];
}
}
}
@end
_______________________________________________
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.


  • Prev by Date: Re: The current keyboard icon?
  • Next by Date: Re: NSView and backgrounds, selectable
  • Previous by thread: Re: setHidden: on MacOS 10.2
  • Next by thread: Re: Too many ports?
  • Index(es):
    • Date
    • Thread