How exactly does an NSView, an NSViewController, a

2019-04-14 14:41发布

问题:

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:

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

  3. In the Attributes inspector, I've entered the name of the Nib I want to load up.

  4. I've connected the view controller object's view outlet to an existing view in MainMenu.xib.

  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.

回答1:

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;