Swift:使用 Interface Builder 创建可重现的 NSView



如果我像这样创建一个NSView子类:

class MyView: NSView {
    var field = NSTextField(frame: NSMakeRect(0, 0, 35, 22))
    func drawRect(dirtyRect: NSRect) {
        super.drawRect(dirtyRect)
        field.stringValue = "Hello World"
        self.addSubview(field)
    }
}

。然后,我可以根据需要创建此视图的任意数量的实例:

let view1 = MyView()
let view2 = MyView()
// ... more instances

。对他们做任何我想做的事:

view2.field.stringValue = "foo"
addSubview(view1)
addSubview(view2)

但是,实际上,我正在尝试创建一个包含多个NSTextField的视图。因此,弄清楚每个NSTextField的帧应该是什么是相当乏味的,更不用说如果我想编辑其中任何一个,事情很快就会变得一团糟。这就是界面生成器派上用场的地方。但是我从来没有遇到过使用接口生成器来创建类声明的方法。我多次创建了一个NSView子类,将"自定义视图"拖到笔尖文件中,向视图中添加了一些内容,从"自定义视图"中的对象创建了一些IBOutlets到我的子类,将"自定义视图"的类更改为我的子类,一切正常。但这是不可重新创建的(至少据我所知)。它只是我的类的一个特定实例,由 IB 初始化,仅此而已 - 只有一个实例。但我想要的是一种重现这种观点的方法。我希望能够在IB中设计一个"模板",然后创建该模板的多个副本。一定有一个好方法可以做到这一点。也许有NSViewController?有什么想法吗?

请参阅这篇文章,了解如何在 swift 中进行 nib 实例化。斯威夫特 - 在课堂上给自己分配一个 NIB

我下面的示例已弃用,但仍然可以正常工作。这是我为此使用了大约 50 次(在目标 C 中)的设计模式。关键的"技巧"是调用"alloc"而不调用"init",就像这个例子一样:

NSViewController *nibFileOwner = [NSViewController alloc];
NSArray *theTopLevelObjects;
[theMbRemoteControlListNib instantiateNibWithOwner:nibFileOwner
                                       topLevelObjects:&theTopLevelObjects];
处理

布局问题仍然很痛苦,但至少笔尖可以处理一些问题。

在这个特定的例子中,我创建了一个比较工具来比较"服务器A"配置和"服务器B"配置。这只是此实现的一个框架。

// subclass as NSMatrix to support drawing reuse of resetButtonCell (BGHUDButtonCell)
@interface MbRemoteControlList : NSMatrix <NSTextFieldDelegate, NSMenuDelegate, EditingAlignmentProtocol>
{
   NSArray                     *mTopLevelObjects;
   IBOutlet NSViewController   *toFilesOwner; // nib files owner
   IBOutlet NSTextField        *toFixNameText;
}
+ (MbRemoteControlList *)_privateCreate
{
    ////////////// BUILD MbRemoteControlList /////////////////////////////////
    NSBundle *theClassBundle;
    theClassBundle = [NSBundle mainBundle];
    NSNib *theMbRemoteControlListNib = [[NSNib alloc] initWithNibNamed:@"MbRemoteControlList" bundle:theClassBundle];
    // A simple NSViewController is the nib 'File's Owner' so we can access
    // the MbRemoteControlList view in the nib hierarchy.
    NSViewController *nibFileOwner = [NSViewController alloc];
    NSArray *theTopLevelObjects;
    [theMbRemoteControlListNib instantiateNibWithOwner:nibFileOwner
                                       topLevelObjects:&theTopLevelObjects];
    MbRemoteControlList *newObj = (MbRemoteControlList *)[nibFileOwner view];
    if ( nibFileOwner != newObj->toFilesOwner )
    {
        DLogErr( @"MbRemoteControlList creation error");
        return nil;
    }
    newObj.mTopLevelObjects = theTopLevelObjects;
    [theMbRemoteControlListNib autorelease];
    if ( newObj.mTopLevelObjects )
        return newObj;
    DLogErr( @"MbRemoteControlList creation error");
    return nil;
}
+ (MbRemoteControlList *) create
{
    setupLayout();
    sLeftServer = [MbRemoteControlList _privateCreate];
    if ( !sLeftServer ) return nil;
    MbRemoteControlList *rightServer = [MbRemoteControlList _privateCreate];
    if ( !rightServer ) return nil;
    [sLeftServer setupWithRightServer: rightServer];
    return sLeftServer;
}

此实现的相关布局 gak:

// sLeftServer is the owner of the active server list
static MbRemoteControlList *sLeftServer;
static BOOL sIsComparingNow = NO; // true if side-by-side comparing with another servers prefs
static BOOL sInitDone = NO;
#define kPrefsUseSmallFont @"kPrefsUseSmallFont"
static BOOL sSmallSize = NO;
static int sOrigBaseWidth = 701;
// action menu items
enum
{
    kLargeFontSize = 1,
    kSmallFontSize = 2
};

typedef struct LayoutStruct
{
    NSControlSize controlSize;
    int rowHeight;
    int fontSize;
    int resetColumnWidth;
    int prefValueWidth;
    int contentWidth;
    int copyingControlsWidth;
    int scrollViewWidth1;
    int rightSideOriginX;
    int toCommandsPopupOriginX;
    int scrollViewWidth2;
} _LayoutStruct;
static LayoutStruct sLayout;
// layout constants
// preference name & value 250
// list view  250 + 20 = 270
// total window
// 6 + 270 + 60? + 282 + 18 + 6
//    #define kResetColumnWidth        20   // (needs to match value in nib + 1)
//    #define kPrefValueWidth          250  // (needs to match the nib)
//    #define kContentWidth            (kResetColumnWidth + kPrefValueWidth + 6)
//    #define kCopyingControlsWidth    120
//    #define kScrollViewWidth1        (kContentWidth + 18)
//    #define kRightSideOriginX        (kContentWidth + kCopyingControlsWidth)
//    #define kScrollViewWidth2        (kRightSideOriginX + kContentWidth + 18)
//
//    #define kDefaultRowHeight         20
static float sWidthFactor = 1.0;
#define textFieldStringWidth (int(100 * sWidthFactor))
#define textFieldFloatWidth  (int(40 * sWidthFactor))
#define textField5DigitWidth (int(39 * sWidthFactor))
#define textField4DigitWidth (int(33 * sWidthFactor))
#define textField3DigitWidth (int(27 * sWidthFactor))
void setupLayout()
{
    sSmallSize = [[[NSUserDefaults standardUserDefaults] objectForKey: kPrefsUseSmallFont] boolValue];
    if ( sSmallSize == YES )
    {
        sWidthFactor = 1.0;
        sLayout.controlSize          = NSSmallControlSize;
        sLayout.rowHeight            = 20;
        sLayout.fontSize             = 9;
        sLayout.resetColumnWidth     = 20;
        sLayout.prefValueWidth       = 250;
        sLayout.contentWidth         = sLayout.resetColumnWidth + sLayout.prefValueWidth + 6;
        sLayout.copyingControlsWidth = 120;
        sLayout.scrollViewWidth1     = sLayout.contentWidth + 18;
        sLayout.rightSideOriginX     = sLayout.contentWidth + sLayout.copyingControlsWidth;
        sLayout.scrollViewWidth2     = sLayout.rightSideOriginX + sLayout.contentWidth + 18;
        sLayout.toCommandsPopupOriginX = sLayout.scrollViewWidth2 - 224;
    }
    else
    {
        sWidthFactor = 13.0 / 9.0;
        sLayout.controlSize          = NSRegularControlSize;
        sLayout.rowHeight            = 25; // same as Director kValueEditorViewHeight
        sLayout.fontSize             = [NSFont systemFontSizeForControlSize:NSRegularControlSize];
        sLayout.resetColumnWidth     = 20;
        sLayout.prefValueWidth       = 250 * sWidthFactor;
        sLayout.contentWidth         = sLayout.resetColumnWidth + sLayout.prefValueWidth + 6;
        sLayout.copyingControlsWidth = 120 + 8;
        sLayout.scrollViewWidth1     = sLayout.contentWidth + 18;
        sLayout.rightSideOriginX     = sLayout.contentWidth + sLayout.copyingControlsWidth;
        sLayout.scrollViewWidth2     = sLayout.rightSideOriginX + sLayout.contentWidth + 18;
        sLayout.toCommandsPopupOriginX = sLayout.rightSideOriginX + 8;
    }
}

最新更新