Re: Finding a view (Cocoa theory)
Re: Finding a view (Cocoa theory)
- Subject: Re: Finding a view (Cocoa theory)
- From: Matt Neuburg <email@hidden>
- Date: Sat, 19 Feb 2005 07:30:08 -0800
On Fri, 18 Feb 2005 17:00:01 -0800, Mark Dawson <email@hidden> said:
>Does Cocoa allow finding an NSView? From what I can tell, there isn't
>any unique ID for a view, so from any spot in a view hierarchy, it
>doesn't seem programatically possible to find a view that's either a
>sibling, super, or sub view from any particular view. Is that correct?
No. A view can have a tag. True, that tag is not unique (that is to say,
nothing about Cocoa forces its uniqueness). But since a view can have a tag,
and since every view is related in some way to the others in the view
hierarchy, and since there are methods for walking up and down the hierarchy
and for obtaining a view with a certain tag, you can find the desired view
starting anywhere else in the hierarchy.
Unfortunately walking the hierarchy is not simple. You'd think, for example,
that to walk down the hierarchy it would suffice to search all subviews
recursively. But in fact some things that you think are subviews cannot be
reached in this manner (tab view items); plus, you might be thinking of a
matrix cell as a view, but of course it isn't. It is useful to write a
NSView category that proposes a recursive version of viewWithTag:, something
like this:
- (id) myViewWithTag: (int) i {
id obj = [self viewWithTag: i];
if (obj) return obj;
// didn't find it, look in other ways
if ([self isKindOfClass: [NSTabView class]]) {
// look thru all tabs
NSEnumerator* ee;
NSTabViewItem* tvi;
ee = [[(NSTabView*) self tabViewItems] objectEnumerator];
while ((tvi = [ee nextObject])) {
id obj = [[tvi view] myViewWithTag: i];
if (obj) return obj;
}
} else if ([self isKindOfClass: [NSMatrix class]]) {
NSEnumerator* ee;
id c;
ee = [[(NSMatrix*) self cells] objectEnumerator];
while ((c = [ee nextObject])) {
if ([c tag] == i) return c;
}
} else {
NSEnumerator* ee;
NSView* aView;
ee = [[self subviews] objectEnumerator];
while ((aView = [ee nextObject])) {
id obj;
obj = [aView myViewWithTag: i];
if (obj) return obj;
}
}
return nil;
}
m.
--
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
AppleScript: the Definitive Guide
<http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden