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.
2
Answers
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.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: