[英]Efficiently Access numerous Cocoa Controls
我有一個包含大量控件的界面,請參見下圖。
界面http://www.richardstelling.com/hosted/cocoainterface.png
訪問這些的最佳方法是什么,在我的AppController
類中創建288個IBOutlets
並將它們全部鏈接起來似乎效率很低。
我查看了表格,但看起來似乎很簡單。
這是概念驗證,因此不會發布,因此我願意接受任何想法。 請注意,但是我必須使用Objective-C,因為最終產品將用Objective-C / Cocoa編寫。
注意:
您應該查看NSMatrix 。 這正是它旨在解決的問題。
NSTableView看起來像您需要的UI。 視覺渲染會有所不同,但看起來會更像“ Mac”。
正如Rob所建議的那樣,要么NSMatrix
,要么重新考慮UI,以使它上的控件更少:-)
您可以以編程方式構建整個接口,並在循環中包含幾行代碼:
const int numRows = 11;
const int rowWidth = 400;
const int rowHeight = 20;
const int itemSpacing = 5;
const int nameFieldWidth = 120;
const int smallFieldWidth = 30;
NSMutableArray * rowList = [[NSMutableArray alloc] initWithCapacity:numRows];
int rowIndex;
NSRect rowFrame = [controlView bounds];
rowFrame.origin.y = rowFrame.size.height - rowHeight;
rowFrame.size.height = rowHeight;
NSRect itemRect
for (rowIndex = 0; rowIndex < 11; rowIndex++)
{
// create a new controller for the current row
MyRowController * rowController = [[MyRowController alloc] init];
[rowList addObject:rowController];
[rowController release];
// create and link the checkbox
itemRect = rowFrame;
itemRect.size.width = 20;
NSButton * checkBox = [[NSButton alloc] initWithFrame:itemRect];
[controlView addSubview:checkBox];
[rowController setCheckBox:checkBox];
[checkBox release];
// create and link the name field
itemRect.origin.x += itemRect.size.width + itemSpacing;
itemRect.size.width = nameFieldWidth;
NSTextField * nameField = [[NSTextField alloc] initWithFrame:itemRect];
[controlView addSubview:nameField];
[rowController setNameField:nameField];
[nameField release];
// create and link the smaller fields
itemRect.origin.x += itemRect.size.width + itemSpacing;
itemRect.size.width = smallFieldWidth;
NSTextField * smallField_1 = [[NSTextField alloc] initWithFrame:itemRect];
[controlView addSubview:smallField_1];
[rowController setSmallField_1:smallField_1];
[smallField_1 release];
//.. continue for each item in a row ..
// increment the main rectangle for the next loop
rowFrame.origin.y -= (rowFrame.size.height + itemSpacing);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.