我不确定我是否理解您的要求。弄清楚用户想要多少个 NSTextField 然后用一点 for() 循环来创建它们不是很简单吗?您可能想要跟踪文本字段,所以我可能会这样做:
NSMutableDictionary * interfaceElements = [[NSMutableDictionary alloc] init];
for (NSInteger i = 0; i < numberOfTextFields; ++i) {
//this is just to make a frame that's indented 10px
//and has 10px between it and the previous NSTextField (or window edge)
NSRect frame = NSMakeRect(10, (i*22 + (i+1)*10), 100, 22);
NSTextField * newField = [[NSTextField alloc] initWithFrame:frame];
//configure newField appropriately
[[myWindow contentView] addSubview:newField];
[interfaceElements setObject:newField forKey:@"someUniqueIdentifier"];
[newField release];
}
字典当然不会是这种方法的本地,但我想你明白了。
或者,您可以强制 NSMatrix 为您自动布局。
如果您正在为 iPhone 编写客户端应用程序,那么我强烈建议您查看Settings Application Schema 参考以获取灵感。如果您对此不熟悉,这里有一个简短的介绍:iPhone 允许开发人员将他们的偏好区域从实际应用程序移动到设置应用程序。这是通过创建一个设置包并以一种非常具体的方式构建一个 plist 来完成的。Settings.app 然后发现该 plist,对其进行解析,并根据其包含的定义构建一个接口。您可以使用开关、文本字段(甚至是安全字段)、滑块、组和其他几种界面元素。