Am I missing something?


So I own every single BigNerdRanch book published, in several editions might I add, and I’ve only recently had a chance to break into them. I’m starting with OSX Cocoa native before I get back into iOS coding just to ensure I understand ALL the basics as mentioned in the forward of the Advanced book.

What I’m looking to do is create all my windows using XIBs, both Windows and Views, and orchestrate them on and off screen. What I’ve found is a great explanation of the default technique, but absolutely no code that explains how to do such a thing programmatically. The pseudo code would be something like:

[code]NSWindow *myWindow = (reference to XIB window object);
NSView *mySubView1 = (reference to XIB view object);

(Add mySubView1 to myWindow, display NOW!)[/code]

Thank you in advance for your help. Your books are THE best written books in the industry. PERIOD.


The Dr.


(moved to the View Swapping chapter forum)

To flesh out your pseudo code into actual code, it would look something like this:

NSWindow *theWindow = …;
NSView *theView = …;
[theView setFrame:[[theWindow contentView] bounds]];
[[theWindow contentView] addSubview:theView];
[theWindow makeKeyAndOrderFront:nil];

Does this answer your question? We cover this topic to some degree in the View Swapping chapter, but not this specific case. Instead we use an NSBox to facilitate the view swapping. Glad you’ve enjoyed the books!



This is very helpful, but I think the explicit code that’s missing is the XIB method for all that is explained programmatically. Both your book and O’Reilly’s book suggest an elaborate view swapping class, before explaining the individual commands for perhaps other implementations to be created. What’s not clear to me is this:

I name a XIB “MainWindow.” I want that attached to an NSWindow *mainWindow object without it being the default window. How?

I name a XIB “MainView.” I want that attached an NSView *mainView to my *mainWindow object at a particular x,y. How?

After these basic questions are answered, I’m ready to bake all the fundamental code into a tutorial that handles swapping views based on an array of view objects that swap in and out. The reality is that I might have a screen that has multiple views such as dialog boxes that will float over other views (I make video games, and don’t want actual Window objects messing up the experience).

I found this example in O’Reilly’s book:

MyWindowController* controller; controller = [[MyWindowController alloc] initWithWindowNibName:@"XIBName"]; [controller showWindow:self];

However, when it comes to the same sample code for adding and showing a view, we’re automatically building an all purpose hide / show’er, which is really for something more like an iOS app than a multi-view OSX app. There is a ton of philosophy and theory, but very little actual code for these basic elements. It seems so fundamental, I was surprised not to see the code. I spent most my time in the Custom Views chapter with a lot of examples of how to use the IB to make a XIB, and the methods that a View can take, but not the sample code for implementing them outside of a default window derived from the AppDelegate / .info file.

On page 241, we get three methods that a View can take…no example.

On pages 242 to 245, we get examples of how to build a XIB view using IB.

On pages 246 to 248, we get programmatic examples of drawing a rectangle…still no examples of how to use the XIB view from the previous section.

On pages 248 to the end of the chapter, we get examples of how to add more shapes programmatically.

The chapter ends without the fundamental question answered, how does one use a XIB view created in IB?

For some reason, this is nearly undocumented in every book on OSX. Again, I could be missing a bigger picture that makes my confusion moot. I’m eager to discover that oversight. :slight_smile:


Chapter 12 has proved very helpful. Between 12 and 31 I can probably figure it a solution. Thanks again.


I agree that many books are missing some very obvious fundamental code examples. If you find your answer, or create it, I’d love to see your solution/examples.

I think your assessment of what is taught and what is glaringly missing is spot on.