我对 iOS 相当有经验,但对 MacOS X Cocoa 还很陌生。
我正在使用 InterfaceBuilder 创建 View 层次结构。这与 MainMenu NIB 是分开的。
我还在创建一个 NSViewController。由于 Mac OS X 的工作方式,此 Controller 对象不直接附加到 View 层次结构。
我连接了 IB 中的所有不同导出,使 View 层次结构成为 View Controller 的 View 对象,并附加了一些包含的控件等等。
现在,问题来了:在 iOS 中,我只是从 Storyboard实例化 Controller ,整个套件和 kaboodle 都随之而来。没有大惊小怪,没有困惑。
在 Mac OS X 中,我可以创建 View Controller :
MyAwesomeViewControllerClass *pViewController = [[MyAwesomeViewControllerClass alloc] initWithNibName:nil bundle:nil];
我找不到与 iOS 中的 instantiateViewControllerWithIdentifier: 方法类似的 Mac OS X。这确实是我所需要的。
如果我需要单独实例化它们,我需要弄清楚如何做到这一点。
我不想以编程方式执行此操作。其中涉及到很多自动布局的东西,这是一大堆(而且很精致)的意大利面条。
很可能,我会犯这个错误,并且想花五美元购买一条线索,亚历克斯......
最佳答案
您需要将 NIB 的名称传递给 initWithNibName:bundle:
:
MyAwesomeViewControllerClass *pViewController = [[MyAwesomeViewControllerClass alloc]
initWithNibName:@"MyAwesomeViewControllerClass"
bundle:nil];
我通常会覆盖init
:
- (instancetype)init {
// NOTE: Not [self initWithNibName:bundle:] !!!
self = [super initWithNibName:@"MyAwesomeViewControllerClass" bundle:nil];
if (self) {
// Other init
}
return self;
}
然后:
MyAwesomeViewControllerClass *pViewController =
[[MyAwesomeViewControllerClass alloc] init];
更容易键入,并且不需要 View Controller 的用户知道正在使用的 NIB(即使它很明显)。
关于macos - Mac OS X cocoa : Create NCViewController with View,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23389121/