Beginning iCloud in iOS 5 Tutorial Part 2(转载)

原文地址:http://www.raywenderlich.com/6031/beginning-icloud-in-ios-5-tutorial-part-2

Setting Up the User Interface

The Xcode project template we chose already set up an empty view controller for us. We will extend it by adding the current document and a UITextView to display the content of our note.

Start by modifying ViewController.h to look like the following:

Beginning <wbr>iCloud <wbr>in <wbr>iOS <wbr>5 <wbr>Tutorial <wbr>Part <wbr>2(转载)

We have also marked the view controller as implementing UITextViewDelegate so that we can receive events from the text view.

Next, open up ViewController_iPhone.xib and make the following changes:

  • Drag a Text View into the View, and make it fill the entire area.
  • Control-click the File’s Owner, and drag a line from the noteView outlet to the Text View.
  • Control-click the Text View, and drag a line from the delegate to the File’s Owner.

At this point your screen should look like this:

Adding a text view into Interface Builder

When you are done, repeat these steps for ViewController_iPad.xib as well.

Next, open up ViewController.m and synchronize your new properties as follows:

@synthesize doc; @synthesize noteView;

Then modify viewDidLoad to register for the notification our code will send when our document changes (we’ll add the code to send this notification later):

Beginning <wbr>iCloud <wbr>in <wbr>iOS <wbr>5 <wbr>Tutorial <wbr>Part <wbr>2(转载)

Next, implement the method that gets called when the notification is received as follows:

Beginning <wbr>iCloud <wbr>in <wbr>iOS <wbr>5 <wbr>Tutorial <wbr>Part <wbr>2(转载)

This simply stores the current document and updates the text view according to the new content received.

In general substituting the old content with the new one is NOT a good practice. When we receive a notification of change from iCloud we should have a conflict resolution policy to enable the user to accept/refuse/merge the differences between the local version and the iCloud one. We’ll discuss more about conflict resolution later, but for now to keep things simple we’ll just overwrite each time.

Next, implement textViewDidChange to notify iCloud when the document changes, and modify the app to refresh the data in viewWillAppear as well:

Beginning <wbr>iCloud <wbr>in <wbr>iOS <wbr>5 <wbr>Tutorial <wbr>Part <wbr>2(转载)

This simply stores the current document and updates the text view according to the new content received.

In general substituting the old content with the new one is NOT a good practice. When we receive a notification of change from iCloud we should have a conflict resolution policy to enable the user to accept/refuse/merge the differences between the local version and the iCloud one. We’ll discuss more about conflict resolution later, but for now to keep things simple we’ll just overwrite each time.

Next, implement textViewDidChange to notify iCloud when the document changes, and modify the app to refresh the data in viewWillAppear as well:

Beginning <wbr>iCloud <wbr>in <wbr>iOS <wbr>5 <wbr>Tutorial <wbr>Part <wbr>2(转载)

As above this is not a great practice, because we are going to notify each iCloud about every single change (i.e. each time a character is added or deleted). For efficiency, it would be better to just tell iCloud every so often, or when the user has finished a batch of edits.

There’s just one last step remaining – we need to add the code to send the “noteModified” notification we registered for in viewDidLoad. The best place in this case is the Note class’s loadFromContents:ofType:error, method which is called whenever data are read from the cloud.

So open up Note.m and add this line of code to the bottom of loadFromContents:ofType:error (before the return YES):

Beginning <wbr>iCloud <wbr>in <wbr>iOS <wbr>5 <wbr>Tutorial <wbr>Part <wbr>2(转载)

Now we are really ready! The best way to test this application is the following: install it on two devices and run it on both. You should be able to edit on one and see the changes periodically propagated to the other.

The propagation of changes is not immediate and might depend on your connectivity. In general, for our examples, it should take 5-30 seconds. Another way to check the correctness it to browse the list of files in your iCloud.

It is a bit hidden in the menu. Here is the sequence:

Settings -> iCloud -> Storage and Backup -> Manage Storage -> Documents & Data -> Unknown

If the application works correctly you should see the note we created in our app:

iCloud document for app in Settings

The ‘unknown’ label comes from the fact that the application has not been uploaded and approved on the Apple Store yet.

Also note that users can delete files from iCloud from this screen at-will (without having to go through your app). So keep this in mind as you’re developing.

Congrats – you have built your first iCloud-aware application!

转载于:https://www.cnblogs.com/pengyingh/articles/2339507.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/548632.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

JQuery Datatables 服务端分页简单应用学习

背景介绍 最近在一个简单小项目中碰到需要一个前端数据表格控件&#xff0c;在看了网上的资料后最终选择了JQuery Datatables。Datatables功能及其强大&#xff0c;基本满足我的所有需求&#xff0c;在加上其插件Editor具有inline模式&#xff0c;很多需要直接修改数据的功能不…

Java API 设计清单

为什么80%的码农都做不了架构师&#xff1f;>>> 在设计Java API的时候总是有很多不同的规范和考量。与任何复杂的事物一样&#xff0c;这项工作往往就是在考验我们思考的缜密程度。就像飞行员起飞前的检查清单&#xff0c;这张清单将帮助软件设计者在设计Java API的…

利用FSMT进行文件服务器迁移及整合

当企业文件服务器&#xff08;DFS、共享文件夹等&#xff09;面临硬件更新、系统升级或文件服务器合并的情况时&#xff0c;往往会出现不确定的文件丢失、需要重新设置所有权限、无法将多个文件服务器集成到一台服务器上等问题&#xff0c;为了保证文件服务器的数据完整以及权限…

RHEL 6上KVM的安装配置及使用-将物理接口桥接到桥接器

作业环境服务器端操作系统&#xff1a;Red Hat Enterprise Linux Server release 6.0 (Santiago)KVM&#xff1a;qemu-kvm-0.12.1.2-2.113.el6.x86_64 客户端操作系统&#xff1a;Windows 7KVM管理工具&#xff1a;Xming 6.9 一、安装KVM及相关软件 1、KVM 需要有 CPU 的支持&a…

C#中IEnumerableT.Distinct()将指定实体类对象用Lambda表达式实现多条件去重

背景说明 在EF等ORM框架中需要以List实体类的方式对数据进行大量操作&#xff0c;其中免不了对一些数据进行去重复&#xff0c;而C#中IEnumerable.Distinct()便提供了这一功能。只是对刚开始接触的新人来说比价抽象难以接受&#xff0c;本文会对这一功能进行简要说明&#xff…

C#中利用Linq.Dynamic实现简单的动态表达式构建查询

背景介绍 在ADO.NET中我们可以根据用户输入的查询条件拼接出指定的SQL语句进行查询或者筛选出所需的数据&#xff0c;但是在ORM框架如EF中&#xff0c;我们一般用LINQ操作数据查询&#xff0c;LINQ是否可以像SQL一样拼接查询条件呢&#xff1f;答案是可以的。这一技术叫Linq.D…

C#中IEnumerableT.GroupBy()的简单使用

背景介绍 在实际项目中&#xff0c;对数据进行GroupBy肯定是常用需求之一&#xff0c;特别是采用EF等ORM框架后隔绝了用SQL语句直接操作数据&#xff0c;LINQ中的GroupBy肯定是要掌握的。 首先先对一个字段GroupBy&#xff0c;代码如下&#xff1a; static void Main(string[…

25 个精美的后台管理界面模板和布局

任何系统都会有一个管理后台&#xff0c;好看的管理后台看起来赏心悦目&#xff0c;管理的时候心情也舒畅&#xff0c;本文给大家推荐 25 个制作精美的后台管理界面的模板和布局&#xff0c;你值得拥有。 Free Admin Template Web App Theme Spring Time Free Admin Template F…

C#中IEnumerableT.Aggregate()的简单使用

背景介绍 IEnumerable<T>.Aggregate()在LINQ使用中好像很不起眼&#xff0c;但我个人认为这是十分实用并且强大的&#xff0c;支持自定义聚合操作&#xff0c;方法定义中的Func包含3个TSource参数&#xff0c;分别为下一个执行聚合的元素&#xff0c;当前聚合的元素&…

C#中IEnumerable.OfType()方法的简单使用

背景介绍 OfType的定义十分简单&#xff1a;IEnumerable.OfType(TResult)&#xff0c;如其定义&#xff0c;其中TRsult为所要过滤的类型。由于非泛型集合一律以Object类型存储对象&#xff0c;因此一个非泛型集合可能存储了各种类型&#xff0c;而OfType()方法可以轻松的对指定…

C#中IEnumerableT.Join()和IEnumerableT.GroupJoin()简单使用

背景介绍: 在无主外键关系的表中如果如果要关联就要用Join()和GroupJoin()方法了&#xff0c;我们先看Join()方法&#xff0c;代码如下&#xff1a; static void Main(string[] args) {List<SW_XSDD> sw_xsdd new List<SW_XSDD>(){new SW_XSDD { com_id "…

利用ASP.NET MVC 的默认类型绑定器---将Jquery datatables中的数据强类型绑定到实体类中

背景描述&#xff1a; 本文参考资料&#xff1a;https://blog.csdn.net/honantic/article/details/45913403 阅读了上述博文后对我产生了启发&#xff0c;在ASP.NET MVC 5中如何将大批量的数据比如说表格中的数据传到后台&#xff0c;是否可以像HTML辅助类一样强类型绑定实体…

JavaScript模态对话框类(拖拽时动画)

2010年写了一个模态对话框类&#xff0c;这次进行一些重构和扩充。拖拽时使其有动画效果。接口没变&#xff0c;如下 new ModelDialog({caption 标题 对话框标题(默认)template 主体内容 (默认)dialogCls 对话框className md-dialog(默认)headCls 头部classNa…

C#中其他简单LINQ查询表达式的简单使用介绍

本文主要记录下其他简单LINQ表达式&#xff0c;因为比较简单&#xff0c;记录下以后方便回忆&#xff0c;本文也会持续更新。 一些有用的LINQ扩展方法&#xff1a; LINQ表达式作用是否延迟查询Range生成指定范围内的整数的序列 Repeat生成包含一个重复值的序列 Skip跳过指定数…

支持上传文件的xhEditor for Typecho EX插件

2019独角兽企业重金招聘Python工程师标准>>> Typecho是一套超轻量的开源博客&#xff0c;界面简洁&#xff0c;功能紧凑&#xff0c;但是Typecho的文本编辑器实在是不好&#xff0c;需要自己写html代码&#xff0c;插图也不方便。试用了几个插件&#xff0c;发现Tin…

C#中IEnumerableT.Select()、SelectMany()的简单使用

本文主要用来记录、让自己有所了解和提升&#xff0c;以后遗忘时可以查看&#xff0c;关于SelectMany()&#xff0c;这篇文章写得不错&#xff0c;值得一看。 话不多说&#xff0c;先上代码看 Select() public class Person {public string Name { get; set; }public string G…

C#中合并两个lambda表达式

在LINQ中如何查询条件不固定&#xff0c;如何合并两个lambda表达式&#xff1f;其中一个方式是LINQ.Dynamic&#xff0c;关于LINQ.Dynamic的简单使用可以参考这篇文章&#xff0c;还有一种方法是利用Expression表达式树&#xff0c;有关表达式树的介绍&#xff0c;可以看这篇文…

彻底理解 Cookie、Session、Token

发展史 1、很久很久以前&#xff0c;Web 基本上就是文档的浏览而已&#xff0c; 既然是浏览&#xff0c;作为服务器&#xff0c; 不需要记录谁在某一段时间里都浏览了什么文档&#xff0c;每次请求都是一个新的HTTP协议&#xff0c; 就是请求加响应&#xff0c; 尤其是我不用记…

C#利用反射实现动态加载程序集简单案例

反射可以不但用来读取元数据&#xff0c;还可以使用反射从编译时还不清楚的类型中动态创建程序集&#xff0c;此案例摘自C#高级编程。 首先先创建一个控制台应用程序&#xff0c;然后添加一个类库&#xff0c;类库名称为CalculatorLib&#xff0c;如下图所示&#xff1a; Calcu…