Finding a view (Cocoa theory)
-
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?
So, if you want to use any particular view, you need to set it up as
an outlet, correct?
Thanks,
Mark -
Hi Mark,
You could subclass NSView and use tags (override the tag method)
together with NSView's subviews and superview methods to do this. Or
you could create your own system of giving NSViews IDs--if it's a
custom class, you can of course do anything you want! I can see this
making sense if you're managing a bunch of dynamically created views.
For most everyday tasks, however, an outlet is probably easiest and
best.
That answer your question?
--Ed
On Feb 18, 2005, at 5:00 PM, Mark Dawson wrote:> 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? So, if you want to use any particular view, you need to set
> it up as an outlet, correct?
>
> Thanks,
>
> Mark
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Cocoa-dev mailing list (<Cocoa-dev...>)
> Help/Unsubscribe/Update your Subscription:
> http://lists.apple.com/mailman/options/cocoa-dev/<ebaskerv...>
>
> This email sent to <ebaskerv...>
> -
On Fri, 18 Feb 2005 17:00:01 -0800, Mark Dawson <marc...> wrote:> 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?
> So, if you want to use any particular view, you need to set it up as
> an outlet, correct?
Generally, views shouldn't need to know about each other. The
Controller layer should deal with the interactions, and normally
you'll use outlets.
What are you actually trying to do?
-- Finlay -
Hi,
I am a little confused by your question: from any spot in the view
hierarchy, what other view would you be looking for? How would you
know if you had found it? E.g., if you would be looking for a view via
a "unique ID", how would your original view get the id in the first
place, and if it got the id at some point in a program, couldn't it
just as easily have gotten a reference to the view?
Maybe these methods are of use?
NSView:
- ancestorSharedWithView:
-Â isDescendantOf:
NSWindow:
- windowNumber
Also check the function NSWindowList at:
http://developer.apple.com/documentation/Cocoa/Reference/
ApplicationKit/ObjC_classic/Functions/AppKitFunctions.html
This mentions NSConvertWindowNumberToGlobal:
http://www.toodarkpark.org/computers/objc/AppKit/Functions/
AppKitFunctions.html
Lastly some window server/NSApp reference:
http://www.oreilly.com/catalog/learncocoa/chapter/ch06.html
On Feb 18, 2005, at 16:00, Mark Dawson wrote:> 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? So, if you want to use any particular view, you need to set
> it up as an outlet, correct?
>
> Thanks,
>
> Mark
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Cocoa-dev mailing list (<Cocoa-dev...>)
> Help/Unsubscribe/Update your Subscription:
> http://lists.apple.com/mailman/options/cocoa-dev/
> <carl.gieringer...>
>
> This email sent to <carl.gieringer...>
> -
(Sorry for sending last email twice; I often forget to include the list
in my replies...)
I see what you mean, now. Look at NSControl's - tag method.
http://developer.apple.com/documentation/Cocoa/Reference/
ApplicationKit/ObjC_classic/Classes/NSControl.html
Although I have not used tags, they appear to be exactly what you need.
You can set the tags programmatically (setTag) or in IB via the
attributes in the inspector. You would need some reference to your
inspector's window, and then you could recursively examine it's
contentView's subviews, seeing if the tag is any particular value that
you set it.
(I am glad that this subject came up; I have a window with which I
could use this tag method to enable/disable controls in it...)
On Feb 18, 2005, at 17:10, mark wrote:> Well, that's my point--I don't see any way in IB to "tag" a view so
> that I
> can find it when searching (how to find one checkbox vs another or one
> NSView vs another NVSView).
>
> What I'm trying to do is build a list of NSViews that contain controls
> (text, checkboxes) that I can hide/show/move around in an inspector
> window
> so that only the panes that have valid properties for a given selected
> object are shown (and shown "neatly"). If I could set an "ID" in IB, I
> could then look for that ID in my app, building up a list of those
> "special"
> views. It looks like I can either ref those views via outlets or
> create a
> subclass of NSView that supports IDs. For the amount of work, outlets
> seem
> to be the way to goâ¦
>
> Thanks,
>
> Mark
>
>> From: Carl Gieringer <Carl.Gieringer...>
>> Date: Fri, 18 Feb 2005 17:02:55 -0900
>> To: Mark Dawson <marc...>
>> Subject: Re: Finding a view (Cocoa theory)
>>
>> Hi,
>>
>> I am a little confused by your question: from any spot in the view
>> hierarchy, what other view would you be looking for? How would you
>> know if you had found it? E.g., if you would be looking for a view
>> via
>> a "unique ID", how would your original view get the id in the first
>> place, and if it got the id at some point in a program, couldn't it
>> just as easily have gotten a reference to the view?
>>
>> Maybe these methods are of use?
>>
>> NSView:
>>
>> - ancestorSharedWithView:
>> -Â isDescendantOf:
>>
>> NSWindow:
>>
>> - windowNumber
>>
>> Also check the function NSWindowList at:
>>
>> http://developer.apple.com/documentation/Cocoa/Reference/
>> ApplicationKit/ObjC_classic/Functions/AppKitFunctions.html
>>
>> This mentions NSConvertWindowNumberToGlobal:
>>
>> http://www.toodarkpark.org/computers/objc/AppKit/Functions/
>> AppKitFunctions.html
>>
>> Lastly some window server/NSApp reference:
>>
>> http://www.oreilly.com/catalog/learncocoa/chapter/ch06.html
>>
>> On Feb 18, 2005, at 16:00, Mark Dawson wrote:
>>
>>> 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? So, if you want to use any particular view, you need to set
>>> it up as an outlet, correct?
>>>
>>> Thanks,
>>>
>>> Mark
>>>
>>> _______________________________________________
>>> Do not post admin requests to the list. They will be ignored.
>>> Cocoa-dev mailing list (<Cocoa-dev...>)
>>> Help/Unsubscribe/Update your Subscription:
>>> http://lists.apple.com/mailman/options/cocoa-dev/
>>> <carl.gieringer...>
>>>
>>> This email sent to <carl.gieringer...>
>>>
>>
> -
On Fri, 18 Feb 2005 17:00:01 -0800, Mark Dawson <marc...> 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 = <matt...>, <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> -
The tag method would seem to work; however, if I drag in a "custom view"
from IB to use as my view "container", the only property available is
"Hidden" in the attributes panel. A tag would only be useful if I could set
it in IB; otherwise an outlet seems to be the better solutionÅ
Thanks,
Mark> From: Carl Gieringer <Carl.Gieringer...>
> I see what you mean, now. Look at NSControl's - tag method.
>
> http://developer.apple.com/documentation/Cocoa/Reference/
> ApplicationKit/ObjC_classic/Classes/NSControl.html
>
> Although I have not used tags, they appear to be exactly what you need.
> You can set the tags programmatically (setTag) or in IB via the
> attributes in the inspector. You would need some reference to your
> inspector's window, and then you could recursively examine it's
> contentView's subviews, seeing if the tag is any particular value that
> you set it.
>
> (I am glad that this subject came up; I have a window with which I
> could use this tag method to enable/disable controls in it...) -
On Feb 19, 2005, at 10:37 AM, mark wrote:> The tag method would seem to work; however, if I drag in a "custom
> view"
> from IB to use as my view "container", the only property available is
> "Hidden" in the attributes panel. A tag would only be useful if I
> could set
> it in IB; otherwise an outlet seems to be the better solutionâ¦
The tag property is only available in NSCell, NSControl, and their
subclasses.
___________________________________________________________
Ricky A. Sharp mailto:<rsharp...>
Instant Interactive(tm) http://www.instantinteractive.com -
If I add a "custom view", then change the custom class to descend from
NSControl, IB still doesn't show anything other than the "Hidden" checkbox
in the "Attributes" Info pane. I am I doing something wrong in IB, or is
this an IB bug? Even if I select another object and go back to that (now
NSControl) object, IB still shows just the NSView (Hidden) dataÅ
Thanks!
Mark> From: Ricky Sharp <rsharp...>
>
>> The tag method would seem to work; however, if I drag in a "custom
>> view"
>> from IB to use as my view "container", the only property available is
>> "Hidden" in the attributes panel. A tag would only be useful if I
>> could set
>> it in IB; otherwise an outlet seems to be the better solutionÅ
>
> The tag property is only available in NSCell, NSControl, and their
> subclasses.
> -
On Feb 20, 2005, at 11:09 AM, mark wrote:> If I add a "custom view", then change the custom class to descend from
> NSControl, IB still doesn't show anything other than the "Hidden"
> checkbox
> in the "Attributes" Info pane. I am I doing something wrong in IB, or
> is
> this an IB bug? Even if I select another object and go back to that
> (now
> NSControl) object, IB still shows just the NSView (Hidden) dataâ¦
Look into IBPalettes. While they can be difficult to create, there are
a few decent examples:
/Developer/Examples/InterfaceBuilder/
/Developer/Examples/AppKit/ClockControlPalette/
For all my custom controls, cells, etc. I created IB palettes. There's
a custom inspector for each different object. Many of the inspectors
provide UI to manage inherited properties (e.g. hidden, enabled, tag)
as well as to manage custom properties.
___________________________________________________________
Ricky A. Sharp mailto:<rsharp...>
Instant Interactive(tm) http://www.instantinteractive.com


