如果我像这样创建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
?有什么想法吗?
答案 0 :(得分:0)
有关如何在swift中执行nib实例化的信息,请参阅此文章。 Swift - Assign a NIB to self in class
以下我的示例已被弃用,但仍然可以正常运行。 这是一个设计模式,我用了大约50次(在目标C中)。 关键的诀窍'是打电话给' alloc'没有' init',就像这个例子:
NSViewController *nibFileOwner = [NSViewController alloc];
NSArray *theTopLevelObjects;
[theMbRemoteControlListNib instantiateNibWithOwner:nibFileOwner
topLevelObjects:&theTopLevelObjects];
处理布局问题仍然很痛苦,但至少nib会处理一些问题。
在这个特定示例中,我创建了一个比较工具来比较服务器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;
}
}