博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iCloud4_Implementing the Detail View Controller
阅读量:7170 次
发布时间:2019-06-29

本文共 6325 字,大约阅读时间需要 21 分钟。

实现详情视图控制器Implementing the Detail View Controller

主视图控制器可以生成 URLs,你现在需要一种方式建立和管理实际文件。你的Xcode项目包含一个详细视图控制器,用来展示主视图控制器上选中的内容。在这一章,你将使用详细视图控制器创建一个文档对象,展现文档当前的文本,提供一种方式来编辑文本,并将文本保存到磁盘。With the master view controller able to generate URLs, you now need a way to create and manage the actual files at those URLs. Your Xcode project includes a detail view controller for displaying the contents of item selected in the master view controller’s table. In this chapter, you will use that detail view controller to create a document object, display the document’s current text, provide a way to edit that text, and save the text back to disk.

 

配置详情视图Configuring the Detail View

默认的视图提供了详细视图控制器包含一个标签对象。文本编辑,标签不能提供足够的功能,需要用文本视图替换,支持多文本输入。The default view provided for the detail view controller contains a single label object. For text editing, the label is not sufficient and needs to be replaced with a text view, which supports multiline text entry.

在设置视图本事之前,你需要准备详细视图控制的源文件。你需要使得类符合STESimpleTextDocumentDelegate协议,还有一些清理要做,不使用标签对象。Before setting up the view itself, you need to to prepare the detail view controller source files. You need to make the class conform to theSTESimpleTextDocumentDelegate protocol and there is also some cleanup you need to do to account for not using the label object.

为文本视图准备源文件
To prepare the source files for the text view

现在,源文件配置好了,你可以添加文本视图到storyboard文件,连接适当的连线。详细视图控制器除了有一个文本视图的连线外,还作为文本视图的代理实现。所有的连线必须都配置在对应的storyboard中。Now that the source files are configured, you can add the text view to the storyboard file and connect the appropriate outlets. In addition to the detail view controller having an outlet for the text view, it also acts as the delegate for the text view. Both outlets must be configured in the storyboard.

添加文本视图到storyboard文件
To add the text view to the storyboard file

 

准备连线Preparing for the Segue

当用户点击主视图控制器表格的一个单元格时,相关的连线加载一个新的详细视图控制器,并展现给用户。无论如何,在详细视图控制器之前就确定展现了,主视图控制器的prepareForSegue:sender:方法被调用,可以将需要传递的数据通过这个方法传给详细视图控制器。When the user taps a cell in the master view controller’s table, the associated segue loads a new detail view controller and displays it to the user. However, before the detail view controller is actually displayed, the master view controller’s prepareForSegue:sender: method is called to give it a chance to pass any needed data to the detail view controller.

这个简单文本编辑应用中,使用prepareForSegue:sender:方法分配选中的URL对象到详细视图控制器。The Simple Text Editor app uses the prepareForSegue:sender: method to assign the selected URL object to the detail view controller.

在连线中配置详细视图控制器
To configure the detail view controller during a segue

虽然详细视图控制器现在有一个到适当的URL的参考,还需要创建文件,或者打开。视图控制器会在它的视图要出现在屏幕时做这件事。Although the detail view controller now has a reference to the appropriate URL, it needs to create the document and either open it or create it. The view controller does this when its view is about to appear onscreen.

为文本视图设置最初的文本
To set the initial text for the text view

当打开一个存在的文件,它通常需要时间从磁盘加载文件内容。在文件加载时,不是延迟展示相应的视图,而是像平常一样加载。初始时,文本视图不包含任何内容。无论如何,因为详细视图控制器指定自己是文件的代理,当文档内容完成加载就会通知。然后会调用documentContentsDidChange代理方法来修改文本视图。When opening an existing document, it usually takes some time to load the document’s content from disk. Rather than delay the presentation of the corresponding views while the document loads, the detail view controller presents its views as usual. Initially, the text view contains no content. However, because the detail view controller assigned itself as the delegate of the document, it is notified when the document content has finished loading. It then uses the documentContentsDidChange: delegate method to update the text view.

当文件完成加载,修改文本视图
To update the text view when the document finishes loading

 

当用户结束编辑时保存文件Saving the Document When the User Has Finished Editing

当用户点击返回按钮,回到文件列表,详细视图控制器将当前的改变写入文件对象,并将文件关闭。关闭文件会将所有改变异步的写入磁盘。When the user taps the back button to return to the list of documents, the detail view controller writes the current changes back to the document object and then closes the document. Closing the document causes any changes to be written to disk asynchronously.

将改变保存到文件
To save changes to the document

 

处理键盘通知Handling Keyboard Notifications

不管键盘何时出现,它滑过你的应用窗口的顶部,隐藏底部的内容。在简单文本编辑应用中,者意味着很大一部分文本视图变成了部分隐藏。指示一个问题,如果用户输入足够多的文本,会引起光标移动到键盘的下边。确保所有的用户文本时可见的,你可以在键盘出现时调节文本视图内部的内容。Whenever the keyboard appears, it slides over the top of your app’s window, hiding the content underneath. In the Simple Text Editor app, this means that a large portion of the text view becomes partially hidden. This is a problem if the user types enough text to cause the cursor to move underneath the keyboard. To ensure that all of the user’s text is visible, you can adjust the content insets of the text view when the keyboard appears.

首先,找出何时键盘出现或隐藏。为了做到这样,注册键盘的通知。First, find out when the keyboard appears and disappears. To do that, you register for the keyboard notifications.

注册键盘通知
To register for the keyboard notifications

就在动画到位之前,键盘UIKeyboardWillShowNotification 类的通知发送。详细视图控制器使用这个通知动画的改变文本视图的内容的插入。添加底部插入会引起文本朝着文本视图的顶部移动。The UIKeyboardWillShowNotification notification is sent shortly before the keyboard is animated into place. The detail view controller class uses this notification to animate the changing of the text view content insets. Adding to the bottom inset causes the text to be shifted toward the top of the text view.

当键盘显示时调整你的文本视图
To adjust your text view when the keyboard is shown

在键盘消失之前,你需要存储内容插图到它们原始的值,这样,内容会再次填满文本视图。Before the keyboard disappears, you need to restore the content insets to their original values so that the content fills the text view again.

当键盘隐藏时,重存文本视图的内容插图
To restore the text view’s content insets when the keyboard is hidden

当改变视图适应键盘,推荐你总是使用的动画。这样做有助于防止你的内容的外观的突然改变。It is recommended that you always use animations when changing views to accommodate the keyboard. Doing so helps prevent abrupt changes to the appearance of your content.

 

要点重述Recap

本章指导中,你创建了文件对象,用它打开或创建相应的文件。你也学会如何保存改变返回到文件,并准备连线。在下一章,你将学到如何搜索到你已经创建好的文件。In this chapter, you created the document object and used it to open or create the corresponding file. You also learned how to save changes back to the document and prepare for segues. In the next chapter, you will learn how to search for the documents you have already created.

转载于:https://www.cnblogs.com/zyingn/articles/iOS_translation13.html

你可能感兴趣的文章
mongo 手册阅读笔记
查看>>
viewport ——视区概念
查看>>
情商的管理
查看>>
页面跳转到Area区域连接
查看>>
拓扑规则翻译函数(转)
查看>>
[转载]SVN使用教程
查看>>
android 获取所有SD卡目录
查看>>
A trick in loading Fixture to test django application
查看>>
经典SQL语句
查看>>
canvas 画椭圆
查看>>
NormalMap
查看>>
java中的注解(一)
查看>>
linux 02 基础命令
查看>>
表单提交中get与post的区别
查看>>
@Transactional注解
查看>>
erlang 时间处理
查看>>
Ubuntu安装pintos
查看>>
Retrofit原理学习
查看>>
hdu Dropping tests 0/1分数规划(二分求值)
查看>>
source命令
查看>>