【问题标题】:Swift: Create Reproducible NSView using Interface BuilderSwift:使用 Interface Builder 创建可重现的 NSView
【发布时间】:2016-01-18 18:47:31
【问题描述】:

如果我像这样创建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 的每个框架应该是什么是相当乏味的,更不用说如果我想编辑其中任何一个,事情很快就会变得一团糟。这就是 Interface Builder 派上用场的地方。但我从未遇到过使用 Interface Builder 创建类声明的方法。我多次创建了NSView 的子类,将“自定义视图”拖到 nib 文件中,在视图中添加了一些内容,从“自定义视图”中的对象创建了一些 IBOutlets 到我的子类,更改了我的子类的“自定义视图”类,一切正常。但这不是可重新创建的(至少据我所知)。它只是我的类的一个特定实例,由 IB 初始化,仅此而已 - 只有一个实例。但我想要的是一种重现这种观点的方法。我希望能够在 IB 中设计一个“模板”,可以这么说,然后创建该模板的多个副本。必须有一个好的方法来做到这一点。也许是NSViewController?有什么想法吗?

【问题讨论】:

  • 您可以根据需要多次反序列化 .xib 文件,并且每次都会得到新的、未共享的对象。查看NSNib

标签: swift interface-builder nsview


【解决方案1】:

请参阅这篇文章了解如何在 swift 中进行 nib 实例化。 Swift - Assign a NIB to self in class

我下面的示例已弃用,但仍然可以正常工作。 这是我为此使用了大约 50 次(在目标 C 中)的设计模式。 关键的“技巧”是在没有“init”的情况下调用“alloc”,如下例:

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

处理布局问题仍然很痛苦,但至少 ni​​b 处理了一些问题。

在这个特定的示例中,我正在创建一个比较工具来比较“服务器 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;
    }
}

【讨论】:

    猜你喜欢
    • 2015-01-03
    • 2017-06-27
    • 1970-01-01
    • 2012-02-25
    • 2013-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多