问题描述
我想知道如何创建一个带有侧边栏的窗口,基本上我想要的是一个侧边栏,它可以通过单击名称来打开一些窗口,如下例所示,提前致谢.
I would like to know how I can create a window with the sidebar, basically what I want is a sidebar that will open some windows by clicking on a name, as in the example below, thanks in advance.
推荐答案
好的,这里是在 Xcode AppleScript 应用程序中设置侧边栏的步骤.我只是展示功能;留美给你玩.
Ok, here are the steps for setting up a sidebar in an Xcode AppleScript app. I'm just showing functionality; I'll leave beautification for you to play with.
首先(显然),设置 GUI.打开 MainMenu.xib,并从对象库中添加三个项目:
First (obviously), set up the GUI. Open MainMenu.xib, and add three items from the object library:
- 窗口左侧的表格视图(用于显示侧边栏)
- 窗口右侧的自定义视图(用于显示详细视图)
- 左侧对象列表中的数组控制器.
它应该看起来像这样(为清楚起见,红色注释):您将需要设置布局约束以保持侧边栏的宽度不变,并将自定义视图粘在其一侧,但您可以使用它.
It should look something like this (red annotation for clarity):You'll want to set up layout constraints to keep the sidebar a constant width and keep the custom view glued to its side, but you can play with that.
现在我们将设置绑定和其他一些细节.首先,单击左侧的 Array Controller 对象,然后打开右侧的 Utilities 窗格.
Now we'll set up bindings and a few other details. First, click of the Array Controller object on the left, and open the Utilities pane on the right.
- 点击属性"检查器.
- 确保 Mode 为class"且class name"为 NSMutableDictionary
- 向Keys"部分添加两个名称:title"和isHeader"(这些是我们将在字典中使用的键)
- 取消点击选择插入的对象",因为这很烦人.
接下来,单击列表中的表视图,然后再次转到右侧的实用程序窗格.
Next, click on the Table View in the list, then go again to the Utilities pane on the right.
- 单击属性"检查器并将列数设置为 1.
- 单击绑定"检查器,转到表格内容"部分,然后创建两个绑定:
- 在内容中点击绑定到复选框,从弹出窗口中选择阵列控制器,并确保控制器密钥为排列对象
- 在选择索引中进行相同的对象绑定,但使用选择索引作为控制器键.
- Click the 'Attributes' inspector and set the number of columns to 1.
- Click the 'Bindings' inspector, go down to the Table Content section, and create two bindings:
- In Content click the Bind to checkbox, choose Array Controller from the popup, and make sure that the Controller Key is arrangedObjects
- In Selection Indexes make the same object binding but use selectionIndexes for the Controller Key.
接下来,单击列表中的表格单元格视图,然后再次点击实用程序窗格.
Next, click on the Table Cell View in the list, and again to the Utilities pane.
- 单击身份"检查器并将标识符设置为tableItem"(不带引号).这让委托人可以找到这个特定的视图并将其传递给表格.
最后,点击左边的表格视图单元格对象(不是它上面的表格单元格视图对象,或者它下面的第二个表格视图单元格:令人困惑,我知道),然后再次转到右侧的实用工具"窗格.
Finally, click on the Table View Cell object on the left (not the Table Cell View object just above it, or the second Table View Cell just below it: confusing, I know), and go to the Utilities pane on the right, again.
- 单击绑定"检查器,转到顶部的值"部分,然后创建一个绑定:
- 在值中点击绑定到复选框,从弹出窗口中选择表格单元格视图,将控制器键留空,然后设置模型objectValue.title 的关键路径
- Click the 'Bindings' inspector, go to the Value section at top, and create one binding:
- In Value click the Bind to checkbox, choose Table Cell View from the popup, leave the Controller Key blank, and set the Model Key Path to objectValue.title
这种工作方式是我们将字典列表转储到数组控制器中,表视图将拾取它并将一个字典作为其对象值分发给每个表单元格视图,然后表视图单元格将提取数据并呈现出来.
The way this works is that we'll dump a list of dictionaries into the Array Controller, the Table View will pick that up and dole out one dictionary to each Table Cell View as its objectValue, and then the Table View Cells will extract the data and present it.
我们已经完成了 GUI,除了将它连接到脚本之外.现在,转到 Xcode 提供的 AppDelegate 脚本,您需要将其更改为如下所示:
We're done with the GUI, except for connecting it the script. Now, go to the AppDelegate script that Xcode provided, and you'll want to change it to look like so:
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_
中添加了两行来创建记录列表并将其添加到数组控制器中,其余的是表视图调用的委托方法.I added two properties — arrayController and detailView, both with the value 'missing value' (which indicates they are IBOutlets) — that we'll link up to the GUI. I've added two lines in
applicationWillFinishLaunching_
to create a list of records and add it to the Array Controller, and the rest are delegate methods that the Table View calls.要链接 GUI,请返回 MainMenu.xib,单击 Array Controller 对象,在 Utilities 窗格中打开Connections"检查器,然后将 New Referencing Outlet 拖到 Delegate 对象,将其与属性连接数组控制器.对自定义视图执行相同操作,将其连接到属性 detailView.你应该没事了.
To link up the GUI, go back to MainMenu.xib, click on the Array Controller object, open the 'Connections' inspector in the Utilities pane, and drag a New Referencing Outlet to the Delegate object, connecting it with the property arrayController. Do the same for the Custom View, connecting it to the property detailView. You should be good to go.
这篇关于如何在 AppleScript 中使用侧边栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!