2

I'm going to cut right to the chase: my application has grown quite a bit, and now I think it's time for me to do some tidying up. I want to separate some of my views from my MainMenu.xib file into their own Nib file. The part that's tripping me up is the whole "Interface Builder + My Code" thing. Here's what I've done so far:

  1. I've added a view controller proxy object:
    View Controller object in Interface Builder object browser

  2. In the Identity inspector, I've added my view controller's class name to the Custom Class field.
    Identity Inspector in Interface Builder

  3. In the Attributes inspector, I've entered the name of the Nib I want to load up. Nib Name field in Interface Builder

  4. I've connected the view controller object's view outlet to an existing view in MainMenu.xib.
    View Outlet on View Controller in Interface Builder

  5. Finally, I hit Cmd+R, and my view isn't there.
    [Insert image of FFFFUUUUU meme here.]

What am I missing? I've been staring at my Mac day-in and day-out for the last two weeks, so I wouldn't be surprised if I've completely left something out. If anyone sees my n00b ways and would be willing to point me in the right direction, I'd be really grateful. Thanks.

jscs
  • 63,694
  • 13
  • 151
  • 195
Ben Stock
  • 1,986
  • 1
  • 22
  • 30
  • I'm not quite sure what you mean by "my view isn't there". What did you connect as an `IBOutlet` `view` then? You need to explicitly call `addSubview:` on whatever view you want to add your subview to. – IluTov Oct 25 '13 at 19:14

1 Answers1

8

You almost got it.

I published a demo-project on Github, just for you ;)


Finally, you need to actually add the view to the window. Do this in your AppDelegate, or wherever you think is suitable.

- (void)awakeFromNib {
    [self.window.contentView addSubview:self.customViewController.view];
    [self.customViewController.view setFrame:[self.window.contentView bounds]];
}

Or course, you first need to make an outlet for your view controller:

@property (assign) IBOutlet ITCustomViewController *customViewController;
IluTov
  • 6,807
  • 6
  • 41
  • 103
  • 1
    Thanks for this, man! Maybe you should become the documentation writer and demo app creator for Apple. I don't know why everyone thinks Apple's documentation is so great. It's lacking in a lot of areas, and half the time, their demo apps use deprecated methods! I know SO doesn't like it when comments are used for "thanking people," but I don't care: Thanks for taking the time to help a fresh-faced Cocoa Maker, NSAddict! – Ben Stock Oct 27 '13 at 19:47
  • @BenStock Apple documentation actually IS pretty great, they just cover a bit more in-depth stuff. But I agree with you that they aren't a lot of help when you're trying to get going with Cocoa. Back when I started developing with ObjC, I thought this [Youtube-Channel](http://www.youtube.com/user/AppleProgramming) pretty useful. – IluTov Oct 27 '13 at 20:03