XIB中的NSSplitViewController/NSSplitViewItem支持 [英] NSSplitViewController/NSSplitViewItem support in XIBs
问题描述
是否为XIB支持NSSplitViewController/NSSplitViewItem?我只看到NSSplitView
Is there support for NSSplitViewController/NSSplitViewItem for XIBs? I see only NSSplitView
我可以拖放NSViewController并将其子类化为NSSplitViewController吗?如何添加开箱即用的NSSplitViewItem?
Can I just drag&drop NSViewController and subclass it as NSSplitViewController? How do I add NSSplitViewItem that it mostly works out of the box?
我很容易在情节提要中看到对它们的支持.
I can easily see support for them in storyboards.
推荐答案
是的.但是它需要一些接线.
Yes it's possible. But it needs some wiring.
首先添加 NSSplitViewItem
的自定义子类,并将 viewController
属性公开为IBOutlet.编译器将发出警告,因此不要忘记将属性标记为动态.
First add a custom subclass of NSSplitViewItem
and expose viewController
property as IBOutlet. Compiler will throw a warning so don't forget to mark property as dynamic.
@interface MySplitViewItem : NSSplitViewItem
@property IBOutlet NSViewController *viewController;
@end
@implementation MySplitViewItem
@dynamic viewController;
@end
在XIB中添加3个NSViewController对象.其中之一更改为自定义类NSSplitViewController.重要的是要注意,不应添加 NSSplitView
.将 NSViewControllers
连接到它的 views
.还添加2个对象,并添加自定义类 MySplitViewItem
,该类公开了 viewController
并将其连接.
In your XIB add 3 NSViewController objects. One of them change to custom class NSSplitViewController. It is important to note that one should NOT add NSSplitView
. Wire NSViewControllers
to it's views
. Also add 2 objects and add custom class of MySplitViewItem
which has exposed the viewController
and wire it.
最后一步.在加载视图之前,设置 NSSplitViewController
的属性 splitItems
很重要!否则,您会被NSAssert宏所吸引.
Last step. It is important to set property splitItems
of NSSplitViewController
before the views are loaded! Otherwise you are caught with NSAssert macro.
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSNib *nib = [[NSNib alloc] initWithNibNamed:@"Empty" bundle:nil];
NSMutableArray *test = [NSMutableArray new];
NSMutableArray *splitItems = [NSMutableArray new];
NSSplitViewController *controller;
[nib instantiateWithOwner:self topLevelObjects:&test];
for (id object in test) {
if ([object isKindOfClass:[NSSplitViewController class]]) {
controller = object;
}
if ([object isKindOfClass:[NSSplitViewItem class]]) {
[splitItems addObject:object];
}
}
[controller setValue:splitItems forKey:@"splitViewItems"];
[[self window] setContentViewController:controller];
}
这是所有接线正确的证明.请注意,我没有触摸XIB中的委托,它已连接.魔术,我知道.
Here is a proof that everything is wired correctly. Note that I did not touch delegate in XIB and it is wired. Magic, I know.
PS:必须将XIB设置为更喜欢编码器+自动布局.
PS: XIB has to be set to prefer Coder + auto layout.
为什么我更喜欢XIB?因为我们可以创建更大的XIB,而不会受到数据隔离的影响(可以轻松地跨NSViewControllers进行绑定).
Why do I prefer XIB? Because we can create larger XIB which doesn't suffer from data isolation (Easily can do bindings across NSViewControllers).
我还尝试在 viewDidLoad
或 setView
或 awakeFromNib
中添加 splitViewItems
:在 NSSplitViewController
(具有公开的NSSplitViewItem属性).如果有人在这里找到解决方案,将不胜感激.
I have also experimented to add splitViewItems
in viewDidLoad
or setView
or awakeFromNib
: in custom subclass of NSSplitViewController
(with exposed NSSplitViewItem properties). If someone finds solution here it will be greatly appreciated.
仅需要代码的解决方案:
- (NSSplitViewController *)profilesSVC
{
if (!_profilesSVC) {
NSSplitViewController *splitVC = [[NSSplitViewController alloc] init];
ProfilesViewController *profilesVC = [[ProfilesViewController alloc] initWithNibName:@"Profiles" bundle:nil];
NSSplitViewItem *leftItem = [NSSplitViewItem splitViewItemWithViewController:profilesVC];
[splitVC addSplitViewItem:leftItem];
ProfileViewController *profileVC = [[ProfileViewController alloc] initWithNibName:@"Profile" bundle:nil];
NSSplitViewItem *rightItem = [NSSplitViewItem splitViewItemWithViewController:profileVC];
[splitVC addSplitViewItem:rightItem];
_profilesSVC = splitVC;
}
return _profilesSVC;
}
这篇关于XIB中的NSSplitViewController/NSSplitViewItem支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!