gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 08:47:41 25 4
gpt4 key购买 nike

如果我像这样创建 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)
}
}

...然后我可以根据需要创建此 View 的任意多个实例:

let view1 = MyView()
let view2 = MyView()

// ... more instances

...然后用它们做任何我想做的事:

view2.field.stringValue = "foo"

addSubview(view1)
addSubview(view2)

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

最佳答案

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

我下面的示例已被弃用,但仍然可以正常工作。这是我为此使用了大约 50 次(在 objective-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;
}

此实现的相关布局技巧:

// 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;
}
}

关于Swift:使用 Interface Builder 创建可重现的 NSView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34861898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com