Re: Newbie Q: How to implement multiple views occupying the same space?
Re: Newbie Q: How to implement multiple views occupying the same space?
- Subject: Re: Newbie Q: How to implement multiple views occupying the same space?
- From: Mike Davis <email@hidden>
- Date: Fri, 18 Jan 2002 10:10:05 -0500
On Friday, January 18, 2002, at 06:31 AM, cocoa-dev-
email@hidden wrote:
Date: Fri, 18 Jan 2002 02:08:55 -0500
Subject: Newbie Q: How to implement multiple views occupying the same
space?
From: email@hidden
To: email@hidden
Hi,
I'd like to implement a set of views controlled by an NSPopUpButton
or a panel of icons similar to the Network preferences panel or the
System Preferences panel where the bottom (part) of the view is
completely replaced based on your selection in the popup button. Can
this be done with nib files and InterfaceBuilder? or do I have to
manually build up a bunch of subViews (no nib files - <sniffle>) and
call [NSView replaceViewWith: <view-user-picked>] everytime the user
picks a new one (bleah).
It's actually quite an easy thing to do. Here's how I've done it...
Create a preference window in IB and place a box with no title or frame
in it. Link it up to a controller, probably your app controller if you
want a single window like the preferences, or to a NSDocument if you're
interested in multiple instances.
Now, for each view you want to add into it, I'd suggest creating a nib
file with another box in it containing the view you want to display. You
can create the view first and then "Group in Box" to make the container.
Hook each container box to a controller of its own, meaning a controller
per preference.
Now when you want to show a particular preference you instantiate the
preference controller (perhaps from a bundle) and place its view into
the window box with setContentView (from NSBox). Don't forget to do a
retain on the view, btw.
As for how the "Show All" view works. I did it as a custom view
containing custom views with icons/labels in it. These custom views
handle the dragging and clicking. The drag icon is generated by copying
the icon to another NSImage and applying alpha transparency to it.
Although at the time I didn't, it's probably best to make the "Show All"
view a matrix with cells in it.
The shelf view I also did as a custom view, since I did it before
NSToolbar was available, which accepted drags for a custom drag type.
I'm currently looking into using a NSToolbar to replace my old code so
it looks more 10.1 rather than 10.0. I figure it'll have to be a
subclass to accept the dragging, though I've not looked that closely
into it.
As it happens, OmniAppKit has a very similar example, though it misses a
few things and there're a couple of minor glitches. It does have the
"poof" icons though for when items are dragged from the shelf. If you
can, you might want to have a look at how that does it. OmniWeb uses it,
by the look of things.
So far, everything else in Cocoa has seemed incredibly easy using nibs,
so I find it hard to believe that there isn't some really easy way to
accomplish this task as well. What am I missing?? Can you make a nib
file that has disembodied (disenwindowed?) NSView subclasses in it??
Put each preference view in a place holder window of it's own, so you
can edit it in IB. You're not interested in the window in your program
though.
Like I said, create a controller, or at least an owner for the view and
make it the file owner. NSDocument implementations do a similar thing.
If you put each controller and nib in a bundle it'll make your
preference window a very flexible engine similar to Apple's own.
Thanks,
email@hidden
P.S. Is there a Cocoa FAQ?? I mean, somewhere besides
http://beowulf-
underground.org/doc_project/cocoa/cocoa.html
Not that I've seen though I guess there is.