完成主视图控制器Implementing the Master View Controller
现在你的应用有一个文档类,是时候开始创建接口来展示这些文件。简单文本编辑应用中,有一个storyboard文件存储所有的视图和视图控制器。这个storyboard包含一个导航控制器、一个主视图控制器、一个详细视图控制器。这一节焦点就是如何配置主视图控制器和相关的代码。Now that your app has a document class, it is time to start building the interface that will display those documents. The Simple Text Editor app has one storyboard file that stores all of its view controllers and views. This storyboard contains a navigation controller, a master view controller, and a detail view controller. This chapter focuses on how to configure the master view controller and its associated code.
设置主视图控制器是一个过程,包含如下步骤:Setting up the master view controller is a process that includes the following steps:
-
设置视图Set up the view.
-
配置视图控制器的数据结构Configure the view controller’s data structures.
-
完成一些表格数据源方法Implement some table data source methods.
-
编写代码来指定新文件的位置Write code to specify the location of new documents.
-
编写代码,编辑文件列表Write code to edit the list of documents.
配置视图Configuring the View
配置主视图控制器的第一步是在你的应用的storyboard文件中配置它的视图。storyboard文件包含一个主场景和一个详细场景。主场景默认内容包括一个表格视图和一个导航对象。The first step for configuring the master view controller is to configure its view in your app’s storyboard file. The storyboard file contains a master scene and a detail scene. The default contents of the master scene include a table view and a navigation item.
表格视图初始配置是展示静态内容,你需哟啊改成动态内容。你还要创建一个单元格到详细视图控制器的连线。The table view is configured to display static content initially but you must change it to display dynamically generated content. You also need to create a segue between the table cells and the detail view controller.
最后一个小细节是改变主视图控制器的导航条目的标题,以更适合这个应用程序。One last small detail is to change the title of the master view controller’s navigation item to something more appropriate for this app.
完成应用数据结构Implementing the App’s Data Structures
你的应用将它在iCloud中找到的文件列表展示到主视图控制器的表格视图中。因为这个列表会随时间改变,你的应用必须提供表格内容是动态的。主视图控制器因此必须管理数据结构,跟踪是可用的文档。Your app displays the list of documents it finds in iCloud in the master view controller’s table view. Because this list can change over time, your app must provide the table contents dynamically. The master view controller must therefore manage the data structures that track the available documents.
主视图控制器的基本数据结构是一个装着NSURL对象的数组,每个URL代表一个在应用的iCloud容器目录下的一个文件。这个数组存储在被称为documents的成员变量中。The master view controller’s basic data structure is an array of NSURL
objects, in which each URL represents the location of a file in the app’s iCloud container directory. The array is stored in a member variable called documents
.
准备添加一个新的文件到表格Preparing to Add a New Document to the Table
在创建新的文档之前,你必须遵守There are several steps that you must perform before you can create new documents:
-
为新的文档生成一个名字Generate a name for the new document.
-
创建新文档的URL位置Build the URL for the new document’s location.
-
添加一个文档按钮到UI界面Add a new document button to the UI.
这些步骤提供你需要的基础设施,之后创建详细视图控制器文档时要用。These steps give you the infrastructure you need to create the document later in your app’s detail view controller.
为新文档生成一个默认名Generating a Default Name for the New Document
在你创建文档之前,考虑好你要给底层文件什么名字。应用被期望要选择合适的默认的名字,这样用户可以更快开始工作。Before you create the document, think about what name you want to give to the underlying file. Apps are expected to choose appropriate default names so that the user can start working quickly.
在简单文本编辑应用中国,生成一个新的文档名,需要一个简单组合。这个组合,由newUntitledDocumentName方法实现,结合一个静态字符串和一个动态选择的整数,创建一个潜在的文档名。接下来,这个名字会和应用的现有的文档名进行比较。这个方法返回第一个不存在的名字。The Simple Text Editor app generates a new document name using a simple scheme. This scheme, implemented by the newUntitledDocumentName
method, combines a static string with a dynamically chosen integer to create a potential document name. That name is then checked against the app’s existing document names. This method returns the first name it finds that is not in use.
问新文档创建URLBuilding the URL for the New Document
现在,你可以指定唯一的名字,你需要一个方法来为文档相关的文件创建URL。一个选择是,创建本地的文件,在通过调用setUbiquitous:itemAtURL:destinationURL:error:方法把文件移到iCloud中。这项技术对运输/航运应用有优先,因为它保证有一个有效的位置来创建文件。本次是简单的指导,因为只用了iCloud中的文件,建大文本编辑应用是直接在iCloud容器目录下创建文件的。Now that you can specify unique names, you need a method to create the URL for the document’s associated file. One option is to create the file locally and move it to iCloud using the setUbiquitous:itemAtURL:destinationURL:error:
method. This technique is preferred for shipping apps because it guarantees that that there is a valid location for creating the file. For simplicity in this tutorial, and because it only works with files that are in iCloud, the Simple Text Editor app creates its documents directly in the iCloud container directory.
创建好URL之后,主视图控制器修改它的表格,将详细视图控制器压入导航视图的栈中。详细视图控制器会基于新的URL处理文档对象的创造物。addDocument:方法时响应创建URL新文档的动作。After creating the URL, the master view controller updates its table and pushes a detail view controller onto the navigation stack. The detail view controller then handles the creation of the document object based on the new URL. The addDocument:
method is the action method responsible for building the URL for the new document.
注意项目不会在这个点编译。你必须完成下面的步骤。Note that the project will not compile at this point. You must complete the steps in the following section, “Adding a New Document Button,” before the code will compile.
添加一个新文档按钮Adding a New Document Button
现在你有一个动作方法,创建文档,你需要添加一个按钮到用户接口,配置它,然后调用此方法。Now that you have an action method for creating documents, you need to add a button to the user interface and configure it to call that method.
完成表格数据源方法Implementing the Table Data Source Methods
虽然你可以创建新文档,这些文档不会在主场景表格视图出现。因为表格视图是动态获取数据的,你需要实现表格的数据源方法tableView:numberOfRowsInSection:
andtableView:cellForRowAtIndexPath:
。第一个报告可用文档的个数,第二个提供者是的要展示的单元格对象。Although you can create new documents, those documents do not yet appear in the master scene’s table view. Because the table view gets its data dynamically, you need to implement the table’s data source methods—specifically, the tableView:numberOfRowsInSection:
andtableView:cellForRowAtIndexPath:
data source methods. The first method reports the number of documents that are available, and the second provides the actual table cell objects to display.
在简单文本编辑中,表格行数就是文档数组的条目数。实现你的tableView:numberOfRowsInSection方法应该返回数组中的条目数。In Simple Text Editor, the number of rows in the table is equal to the number of entries in the documents
array. The implementation of yourtableView:numberOfRowsInSection:
method should therefore just return the number of entries in the array.
对每一行,数据源提供一个单元格展示文档名。简单文本编辑应用会给单元格使用默认类型。For each row, the data source provides a table view cell that displays the name of the document. The Simple Text Editor app uses the default style for its table cells.
编辑文档列表Editing the List of Documents
最后一个添加到主视图控制器的特性:支持在表格中删除行。你通过使用表格视图内置的编辑支持来删除行。这个特性将会简化你的应用测试。There is one last feature to add to the master view controller: support for deleting rows in the table. You delete rows by using the table view’s built-in editing support. This feature will make it easier for you to test your app later.
第一步是添加一个按钮,用户点击后可以使表格进入编辑模式。在UIViewController类中,提供一个前配置条按钮,可以用它来实现一个编辑按钮。The first step is to add a button that the user can tap to put the table into editing mode. The UIViewController
class provides a preconfigured bar button item that you can use to implement an Edit button.
当用户点击编辑按钮,调用视图控制器的setEditing:animated:方法来开始编辑过程。于此同时,按钮外观变化到完成按钮,提供给用户关于编辑模式改变的反馈。点击完成按钮,退出编辑模式。When the user taps the Edit button, it calls the setEditing:animated:
method of the view controller to begin the editing process. At the same time, the appearance of the button changes to that of a Done button to provide feedback to the user about the change in edit mode. Tapping the Done button exits edit mode.
当主视图控制器在编辑模式,它添加控制到表格,以允许删除个别的行。虽然该控制元件是可见的,可以被用户点击,这样做不会真的删除底层文件。为了解决文件删除,需要实现tableView:commitEditingStyle:forRowAtIndexPath:数据源方法。你的实现要做的是删除文件,然后修改表格的数据结构。When the master view controller is in edit mode, it adds controls to its table to allow for deleting individual rows. Although the controls are visible and can be tapped by the user, doing so does not actually delete the underlying files yet. To handle the file deletion, you need to implement thetableView:commitEditingStyle:forRowAtIndexPath:
data source method. Your implementation of this method must delete the files and update the table data structures.
当要删除或移动iCloud中的文件存储时,总是使用一个NSFileCoordinator对象。这是一个极少数会手动创建文件协调员对象的情况之一。在大多其他情况,文档对象会自动为你创建,并用它来执行与文件有关的操作。文件协调员确保iCloud服务不会在你要删除它时更改文件。还确保了iCloud服务会在你的确要删除文件时受到一个通知。更多信息请查看文件系统编程指导。When deleting or moving files stored in iCloud, always use an NSFileCoordinator
object. This is one of the few times when you must create the file coordinator object yourself. In most other cases, the document object creates one for you and uses it to perform the relevant file-related operation. The file coordinator ensures that the iCloud service cannot modify the file while you are deleting it. It also ensures that the iCloud service is notified when you do delete the file. For more information about using file coordinators, see File System Programming Guide.
扼要重述Recap
在这一章,你为主视图控制器配置表格视图,并添加控制,让你创建、编辑文档的列表。你也可以实现必要的代码,可以在iCloud中创建、删除文档文件。最后,你的配置的一个storyboard连线通过展示详细视图控制器来响应选择的一个文档。在下一章,你将学到如何在详细视图控制器中展示并编辑一个文档。In this chapter, you configured the table view for the master view controller and added controls that let you create and edit the list of documents. You also implemented the code needed to create and delete your document files in iCloud. Finally, you configured a storyboard segue that responds to the selection of a document by displaying the detail view controller. In the next chapter, you will learn how to display and edit the contents of a document using the detail view controller.