好的,以下是在 Xcode AppleScript 应用程序中设置侧边栏的步骤。我只是展示功能;我把美化留给你玩。
首先(显然),设置 GUI。打开 MainMenu.xib,从对象库中添加三个项目:
- 窗口左侧的表格视图(用于显示侧边栏)
- 窗口右侧的自定义视图(用于显示详细视图)
- 左侧对象列表中的阵列控制器。
它应该看起来像这样(为清楚起见,红色注释):
您需要设置布局约束以使侧边栏保持恒定宽度并将自定义视图粘在其一侧,但您可以使用它。
现在我们将设置绑定和其他一些细节。首先,单击左侧的 Array Controller 对象,然后打开右侧的 Utilities 窗格。
- 单击“属性”检查器。
- 确保 Mode 为“class”,“class name”为 NSMutableDictionary
- 在“Keys”部分添加两个名称:“title”和“isHeader”(这些是我们将在字典中使用的键)
- 取消单击“选择插入的对象”,因为它很烦人。
接下来,单击列表中的表格视图,然后再次转到右侧的实用程序窗格。
- 单击“属性”检查器并将列数设置为 1。
- 单击“绑定”检查器,转到“表格内容”部分,然后创建两个绑定:
- 在 Content 中单击 Bind to 复选框,从弹出窗口中选择 Array Controller,并确保 Controller Key 为 排列对象
- 在 Selection Indexes 中进行相同的对象绑定,但使用 selectionIndexes 作为控制器键。
- 单击“Connections”检查器,然后从委托出口拖到左侧的 Delegate 对象,这样表格视图将使用 AppDelegate 脚本作为其表格视图委托。
接下来,单击列表中的表格单元格视图,然后再次进入实用程序窗格。
- 单击“Identity”检查器并将标识符设置为“tableItem”(不带引号)。这让代理可以找到这个特定的视图并将其传递给表格。
最后,单击左侧的 Table View Cell 对象(不是它正上方的 Table Cell View 对象,或者它正下方的第二个 Table View Cell:令人困惑,我知道),然后再次转到右侧的实用程序窗格。
- 单击“绑定”检查器,转到顶部的“值”部分,然后创建一个绑定:
- 在 Value 中单击 Bind to 复选框,从弹出窗口中选择 Table Cell View,将 Controller Key 留空,然后设置 Model objectValue.title 的关键路径
它的工作方式是我们将一个字典列表转储到数组控制器中,表格视图将拾取它并将一个字典分配给每个表格单元视图作为其对象值,然后表格视图单元将提取数据并呈现出来。
我们已经完成了 GUI,除了将它连接到脚本。现在,转到 Xcode 提供的 AppDelegate 脚本,您需要将其更改为如下所示:
script AppDelegate
property parent : class "NSObject"
-- IBOutlets
property theWindow : missing value
property arrayController : missing value
property detailView : missing value
on applicationWillFinishLaunching_(aNotification)
-- Insert code here to initialize your application before any files are opened
(* set up list of headers and lines for the side bar *)
set sidebarList to {{title:"Header 1", isHeader:true}, {title:"Line 1", isHeader:false}, {title:"Header 2", isHeader:true}, {title:"Line 2", isHeader:false}, {title:"Line 3", isHeader:false}, {title:"Header 3", isHeader:true}, {title:"Line 4", isHeader:false}}
arrayController's addObjects:sidebarList
end applicationWillFinishLaunching_
on applicationShouldTerminate_(sender)
-- Insert code here to do any housekeeping before your application quits
return current application's NSTerminateNow
end applicationShouldTerminate_
(* table view delegate emthods *)
on tableView:tableView isGroupRow:row
-- header rows are group rows
set rowData to arrayController's arrangedObjects's objectAtIndex:row
return rowData's isHeader
end
on tableView:tableView shouldSelectRow:row
-- don't want to select header rows
set rowData to arrayController's arrangedObjects's objectAtIndex:row
return not (rowData's isHeader)
end
on tableView:tableView viewForTableColumn:column row:row
-- header rows get a special look
set aView to tableView's makeViewWithIdentifier:"tableItem" owner:me
return aView
end
on tableViewSelectionDidChange:aNotification
(*
This is method gets notified right after a selection is made. This is one of
the places where you can change the detail view to show a the correct view for
selected sidebar item. For demonstration purposes I'm just swapping out
TextField views with the name of the sidebar item. Not to sophisticated,
but it get the point across
*)
set tableView to aNotification's object
set selectedRowIdx to (tableView's selectedRow) as integer
set rowData to arrayController's arrangedObjects's objectAtIndex:selectedRowIdx
set newLabel to current application's NSTextField's labelWithString:(rowData's title )
set newLabel's translatesAutoresizingMaskIntoConstraints to false
set detailSubviews to (detailView's subviews) as list
if detailSubviews is not {} then
set oldLabel to item 1 of detailSubviews
detailView's replaceSubview:oldLabel |with|:newLabel
else
detailView's addSubview:newLabel
end
set constraintX to newLabel's centerXAnchor's constraintEqualToAnchor:(detailView's centerXAnchor)
set constraintY to newLabel's centerYAnchor's constraintEqualToAnchor:(detailView's centerYAnchor)
constraintX's setActive:true
constraintY's setActive:true
end
end script
我添加了两个属性——arrayController 和 detailView,它们的值都是“缺失值”(表示它们是 IBOutlets)——我们将链接到 GUI。我在applicationWillFinishLaunching_ 中添加了两行,用于创建记录列表并将其添加到 Array Controller,其余的是 Table View 调用的委托方法。
要链接 GUI,请返回 MainMenu.xib,单击 Array Controller 对象,在 Utilities 窗格中打开“Connections”检查器,然后将 New Referencing Outlet 拖动到 Delegate 对象,将其与属性连接数组控制器。对自定义视图执行相同的操作,将其连接到属性 detailView。你应该很高兴。