将对象添加到NSView [英] Adding objects to an NSView

查看:123
本文介绍了将对象添加到NSView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目窗口中有一个NSView,如下图所示:

I have an NSView inside my project's window as seen in the picture below:

如何添加NSImageViews到这个'自定义视图'的子视图,所以它显示在那里,而不是直接在主窗口

How do I add NSImageViews to the subview of this 'Custom View' so its displayed in there rather than directly in the main window of the application?

推荐答案

以编程方式,您将访问要添加子视图的视图,然后调用-addSubView:方法就可以了。您可以作为一个IBOutlet连接到接口构建器访问它,或者通过标识符访问它(在接口构建器检查器的身份部分设置标识符字符串)。通过windowcontroller视图的子视图迭代(您的自定义视图将是一个)并测试identifier的字符串值

Programmatically, you would access the view to which you want to add subviews, then call the -addSubView: method on it. You can access it either as an IBOutlet hooked in to interface builder, or access it by Identifier (set the Identifier string in the "identity" section in the interface builder inspector). Iterate through the subviews of the windowcontroller's view (your custom view would be one) and test the string value of "identifier"

这是来自最近项目的简化代码片段以编程方式向已经具有在xib中定义的框的视图中添加额外的属性框。我添加一个新的框视图( propertiesBoxView )到现有的 NSView (引用由IBOutlet- c $ c> _detailsView )并将其相对于另一个同级框(其标识符设置为UI_DETAILSBOX)定位。 NSImageView的行为应该与框视图相同:

Here's a simplified code snippet from a recent project for programmatically adding an extra "properties" box into a view that already has a box defined in the xib. I'm adding a new box view (propertiesBoxView) into an existing NSView (referenced by the IBOutlet-ed _detailsView) and positioning it relative to another sibling box (with its identifier set to "UI_DETAILSBOX"). An NSImageView should behave the same way as a box view:

_propsViewController = [[MySpecialViewController alloc] initWithThings:...];
/* snip */

NSView *propertiesBoxView = [_propsViewController view];
NSView *detailsBox = nil;
// find the details box
for (NSView *sibling in [_detailsView subviews]) {
  if ([[sibling identifier] isEqualToString:@"UI_DETAILSBOX"]) {
    detailsBox = sibling;
    break;
  }
}
if (detailsBox == nil) {
  return;
}

[_detailsView addSubview:propertiesBoxView];
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(propertiesBoxView, detailsBox);
NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[detailsBox]-9-[propertiesBoxView]-(>=9)-|"
                                                             options:0
                                                             metrics:nil
                                                               views:viewsDictionary];
NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-9-[propertiesBoxView]-(>=9)-|"
                                                             options:0
                                                             metrics:nil
                                                               views:viewsDictionary];
[_detailsView addConstraints:verticalConstraints];
[_detailsView addConstraints:horizontalConstraints];

这篇关于将对象添加到NSView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆