原文地址: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:
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:
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):
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:
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:
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):
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:
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!