开发者

NSView high-level overview?

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 t开发者_如何转开发o 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!


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.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜