How exactly does an NSView, an NSViewController, a

2019-04-14 14:43发布

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.

1条回答
不美不萌又怎样
2楼-- · 2019-04-14 14:50

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;
查看更多
登录 后发表回答