How exactly does an NSView, an NSViewController, and MainMenu.xib fit together?

十年热恋 提交于 2019-12-06 11:35:44

问题


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;


来源:https://stackoverflow.com/questions/19583085/how-exactly-does-an-nsview-an-nsviewcontroller-and-mainmenu-xib-fit-together

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!