2011年11月7日 星期一

iCloud in iOS 5 Tutorial Part 2

since: 2011/11/07
update: 2011/11/07

reference: Beginning iCloud in iOS 5 Tutorial Part 2 | Ray Wenderlich

A. Setting Up the User Interface
   1. 修改 ViewController.h 如下:
#import <UIKit/UIKit.h>
//@add
#import "Note.h"

//@interface ViewController : UIViewController
//@update: marked the view controller as implementing UITextViewDelegate
// so that we can receive events from the text view
@interface ViewController : UIViewController <UITextViewDelegate>

//@add
@property (strong) Note * doc;
@property (weak) IBOutlet UITextView * noteView;

@end

---------------------------------------------------------------------------------------------

   2. 開啟 ViewController_iPhone.xib 檔案, 修改如下:
      a. 拖拉一個 Text View 讓它蓋滿整個區域.

      b. 將 File's Owner 上的 noteView outlet 與 UI 上的 Text View 做連結.

      c. 點選 UI 上的 Text View, 將其 delegateFile's Owner  做連結. 

---------------------------------------------------------------------------------------------

   3. 同樣地, 開啟 ViewController_iPad.xib 檔案, 修改如下:
      a. 拖拉一個 Text View 讓它蓋滿整個區域.
      b. 將 File's Owner 上的 noteView outlet 與 UI 上的 Text View 做連結.
      c. 點選 UI 上的 Text View, 將其 delegateFile's Owner  做連結. 

---------------------------------------------------------------------------------------------

   4. 修改 ViewController.m 如下:
//@add
@synthesize doc;
@synthesize noteView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //@add: register for the notification our code will send when our document changes
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(dataReloaded:)
                                                 name:@"noteModified" object:nil];
}

//@add: implement the method that gets called when the notification is received
- (void)dataReloaded:(NSNotification *)notification {
   
    self.doc = notification.object;
    self.noteView.text = self.doc.noteContent;
}

//@add: notify iCloud when the document changes
- (void)textViewDidChange:(UITextView *)textView {
   
    self.doc.noteContent = textView.text;
    [self.doc updateChangeCount:UIDocumentChangeDone];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //@add: modify the app to refresh the data
    self.noteView.text = self.doc.noteContent;
}

---------------------------------------------------------------------------------------------

   5. 修改 Note.m 如下:
- (BOOL)loadFromContents:(id)contents ofType:(NSString *)typeName
                   error:(NSError **)outError
{
    ....
    //@add: add the code to send the “noteModified” notification we registered for in viewDidLoad.
    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"noteModified"
     object:self];
   
    return YES;   
}



B. 測試:
   在二台 iDevice 安裝此 app 與執行; 在其中一台 iDevice 編輯資料, 約 5 ~ 30 秒
   便會在另一台 iDevice 更新資料.

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。