Chapter 16: Hierarchies

Cocoa uses hierarchies of views in its windows, with each window having a content window, containing controls that are themselves views. It’s basically the Composite Pattern as covered in Chapter 9 of Head First Design Patterns. So views have views inside them, which can have view inside them, as deep as you want (or need). There may be more nesting than you first expect: dropping an NSTextView onto a window actually creates an NSScrollView, which contains an NSClipView and two NSScrollers, with the NSClipView holding the NSTextView you originally wanted.

The book’s example program for this chapter is a two-window application. One presents sample user interface elements, while the second has a browser that lets the user select one of the controls in the other window, which will the be outlined in red. Since each top-level control may hold sub-controls, the browser lets the user drill down and select controls at any level.

I didn’t have any big conceptual problems with this chapter, but getting the program to work took a while, mostly looking for connections not made in IB or lines of code I missed when typing the files in.

I decided it would be interesting to use a window from one of my own applications for the browser to examine. The one I chose was from an HTML editor, which had a window with a split view, with the top half containing a text view for the user to edit the HTML, and the bottom half containing a web view, showing the rendered HTML. As the user types, the web page updates in real time.

I copied the window from the original nib, pasted it into the chapter 16 program, and adjusted the outlet of the app delegate to point at the new window. When I ran the program, the new window wasn’t visible, although the browser woud draw a red ouline where the selected object would have been drawn. The Window→Bring All to Front command didn’t help, nor did cmd-tilde, which just rotated between the browser window and the original observed window. Going back to the nib file, I noticed that Visible At Launch wasn’t checked. I checked the box, ran the program, and there was the window!

The interesting thing I noticed from the looking at my window was the position of one of the scrollers. When there was no text in the window, it was off to the left of the window (x=–30 in window coordinates). After pasting in enough text that the scroller was needed, it moved to the appropriate spot on screen. I had though Cocoa would simply set a “don’t display” flag, instead of moving it outside where it would be clipped.

Advertisements
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s