views:

21

answers:

1

I am trying to create a custom view and load it into a window.

In the end, I know there will be a few steps to follow, reflecting the key relationships that I am missing. I am hoping someone can explain, in a series of steps, how one creates a view object, view controller, associated .xib, and then loads it into a window (after clearing the current view).

And I mean the real nitty gritty of where to declare and initialize, what needs to be imported, everything. Because I am looking through every book I have and it is just not clear to my puny brain.

Thanks!

A: 

how one creates a view object, view controller, associated .xib, and then loads it into a window …

These are several things, and some of them conflict.

If you create a view in code, you don't need to (and shouldn't) also create it in a nib, and vice versa.

If you create a view controller to load the nib, you will be creating the view in a nib, so you definitely should not create the same view in code.

You do not need to create a view controller for most views. It is more common to have each controller own the entirety of exactly one window. The only time you need view controllers is when you manage a complex view hierarchy in a single window (most likely if you make your application single-window).

… (after clearing the current view).

There is no “current view” in Cocoa. You can have multiple windows, and each has a deep view hierarchy that you usually don't edit at run time. Swapping one view for another outside of some sort of tabbed UI is very unusual.

Creating a view object in code

Send the desired view class an alloc message and the returned view an initWithFrame: message (unless otherwise prescribed by the class's documentation). You will, of course, need to release or autorelease this view.

Creating a view object in a nib

Giving it its own nib (especially for view controllers)

Use the view-nib template in IB (New) or Xcode (Add File). If you create it in Xcode, don't forget to get info on it and make it localizable. If you create it in IB, you should save it into one of your .lproj folders; then it will already be localizable.

A nib created from those templates will contain one empty NSView. You can change its class and/or add subviews as described below.

Making it in an existing nib

Drag “Custom View” from the Library palette into the nib window, then set the view's class on the ⌘6 inspector.

You only do this for the top-level view in the nib. For its subviews, see below.

Putting the view into a window's view hierarchy

If the view should be the root of the window's view hierarchy (the window's content view)

Set the window's content view.

In IB, you can't change the window's content view. Instead, you change things about it—its class, subviews, etc. There is no reason to try to replace the window's content view with another view in IB.

If the view should be a subview of an existing view

The way to do this in code is to send the superview an addSubview: message.

If both views are in the same nib, create the subview and add it to the superview in the same act. Drag “Custom View” from the Library into the superview, not the nib window, then set the subview's class on the ⌘6 inspector.

(If you're customizing one of the standard Apple views, rather than making a completely original custom view, drag the standard Apple view you based yours on from the Library, then change its class to your customized subclass.)

Peter Hosey
Thanks again Peter. I want to follow the example of having a custom view at the root of the window, and then add subviews to it when certain events occur, so if I understand correctly, this involves dragging a custom view into the NSWindow, and then having 2 custom views as their own objects. But man...I don't get about setting any of these view's classes. Just in IB? Or do they have to have corresponding view class files? And if they do, what should those class files contain? Nothing? btw I am accepting this answer because, going with history, I'm sure I can trust it!
Alec Sloman
You can't just make up a class name in IB and will the class into existence; you must implement the class in your program's source code. Xcode has a template for an NSView subclass: It's one of the superclass options when you add+create the files for a new Objective-C class. You *can* leave it empty, but then it isn't really custom, and you might as well not bother making a custom class—just leave the instance an instance of whatever standard Apple view class you started with. When you make a custom view class, you're always going to customize *something*.
Peter Hosey