如何在 Cocoa MacOS Objective C 中为 NSSavePanel 创建自定义 NSView?
我需要将一个保存扩展选择器添加到我的 NSSavePanel
中,旁边有一个文本标签。在所附的屏幕截图中,我尝试证明我已成功使用 setAccessoryView
函数将 NSComboBox
添加到我的面板中。但是我不知道如何创建自定义 NSView,其中包括 NSComboBox 和 NSTextView 或等效项。我在互联网上没有找到任何教程(或者如果我找到一个它也非常过时)展示如何在 MacOS 上的 Cocoa 中的 Objective-C 中创建自定义 NSView。
如何创建包含组合框和文本标签的自定义 NSView
?或者如何将两个“库存”NSView 添加到同一个 NSSavePanel 中?请尽可能详细地回答,因为我的 Objective-C 经验非常有限。
I need to add a save extension selector with a text label next to it to my NSSavePanel
. In the screenshot attached I try to demonstrate that I succeeded in adding an NSComboBox
to my panel with the function setAccessoryView
. However I have no idea how to create a custom NSView, which includes both an NSComboBox
and an NSTextView
or equivalent. I found no tutorials on the internet (or if I found one it was extremely outdated) showing how to create custom NSView
s in objective-C in Cocoa on MacOS.
How can I create a custom NSView
containing a combobox and a text label? Or how can I add two "stock" NSView
s to the same NSSavePanel
? Please be as detailed in your answer as possible, as I have very limited objective-c experience.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您询问如何在 Objective-C 中创建一个
NSView
并使用NSTextField
和NSComboBox
作为子视图。基本上,您可以在 Interface Builder 中定义它们,并以编程方式将 Objective-C 中的结果视图设置为
NSSavePanel
的accessoryView
。或者,可以完全用 Objective-C 创建自定义 NSView,这可能是更简单的选择。实例化
NSView
后,您可以使用addSubview:
相应地添加NSTextField
和NSComboBox
。然后,您可以使用 NSLayoutConstraints 设置自动布局,它负责调整 accessoryView 的大小并根据对话框的宽度正确排列子视图。如果您以编程方式创建视图并使用自动布局,则必须将
translatesAutoresizingMaskIntoConstraints 显式设置为
NO
。如果您想设置
allowedContentTypes
,通过NSDictionary
将显示的扩展文本映射到UTType
可能会很有用。如果您将
NSComboBox
的委托设置为self
,那么您将通过通知您
。NSComboBox
中用户选择的更改comboBoxSelectionDidChange:如果所讨论的内容在代码中得到了适当的实现,那么对于一个独立的示例来说,它可能看起来像这样:
最后,上面的演示代码的屏幕截图看起来像这样:
You asked how to create an
NSView
in Objective-C with anNSTextField
and anNSComboBox
as subviews.Basically, you could define them in Interface Builder and programmatically set the resulting view in Objective-C as the
accessoryView
of theNSSavePanel
. Alternatively, the customNSView
could be created entirely in Objective-C, which is probably the easier option here.After instantiating an
NSView
, you can useaddSubview:
to add anNSTextField
and anNSComboBox
accordingly. Then you can useNSLayoutConstraints
to set up Auto Layout, which takes care of sizing theaccessoryView
and arranging the subviews properly based on the width of the dialog.If you create the views programmatically and use Auto Layout, you must explicitly set
translatesAutoresizingMaskIntoConstraints
toNO
.Should you want to set the
allowedContentTypes
, a textual mapping of the displayed extension toUTType
via aNSDictionary
might be useful.If you set the delegate of the
NSComboBox
toself
, then you will be informed about changes of the user selection in theNSComboBox
viacomboBoxSelectionDidChange:
.If the things discussed are implemented appropriately in code, it might look something like this for a self-contained example:
Finally, a screenshot of the above demo code in action looks like this:
按 Cmd-N 将新文件添加到您的项目中。选择一个视图文件以添加具有自定义视图的 xib 文件。
打开 xib 文件并将控件添加到自定义视图。按项目窗口工具栏中的“添加”按钮以访问用户界面元素。
使用
NSNIb
类加载xib文件并获取自定义视图。Press Cmd-N to add a new file to your project. Choose a View file to add a xib file that has a custom view.
Open the xib file and add the controls to the custom view. Press the Add button in the project window toolbar to access the user interface elements.
Use the
NSNib
class to load the xib file and get the custom view.