2013-04-10 90 views
1

確定。我肯定錯過了什麼。設置或使用Firebase的iOS遠程接口鏡像示例

我試圖得到兩個ipad公司,這樣,當一個用戶執行UISwipeGesture推動另一種觀點認爲,這是在其他設備上覆制過去,以反映它們的接口。我希望實時推送實時視圖並在應用的兩個實例中顯示。我一直在審覈並使用Firebase進行測試項目,並沒有看到如何解決這個問題。但是,我已經看到了使用Firebase執行此操作的原生iOS應用程序(更不用說JavaScript繪圖示例了)。

沒有人有任何好的代碼示例,也可以點我在正確的方向?

感謝, 道格

回答

1

火力地堡沒有做特殊應用同步等接口鏡像任何特定的原語。您需要做的是將界面的當前狀態模型化爲Firebase中的JSON。因此,隨着用戶界面部分的更改,您可以將它們保存到Firebase中。然後,您還需要設置補充:當由於Firebase更改而觸發事件時,請更新UI。對於諸如手勢和動作之類的東西,需要使用相同類型的建模 - 將這些項目保存在Firebase中(或者,如果這樣做不能在語義上輕鬆轉換,請保留動作的副作用;例如,如果滑動導致按鈕移動位置,保存火力地堡按鈕的位置。)

具體而言,這裏是保持兩個選項卡同步的例子。這是基於默認的Xcode項目模板「Tabbled應用程序」。在這裏面我們做了幾件事情:

  1. 添加志願觀察員當標籤項目改變
  2. 安裝鏡像的變化反應從火力地堡
  3. 實際保存UI的新狀態,以火力

#import <Firebase/Firebase.h> 

#define kFirebase @"https://mirror.firebaseio-demo.com" 

@implementation FAppDelegate 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    UIViewController *viewController1 = [[FFirstViewController alloc] initWithNibName:@"FFirstViewController" bundle:nil]; 
    UIViewController *viewController2 = [[FSecondViewController alloc] initWithNibName:@"FSecondViewController" bundle:nil]; 
    self.tabBarController = [[UITabBarController alloc] init]; 

    // Add KVO observer for when the UI changes so we can eventually synchronize this data to Firebase 
    [self.tabBarController.tabBar addObserver:self forKeyPath:@"selectedItem" options:NSKeyValueObservingOptionNew context:nil]; 
    self.tabBarController.viewControllers = @[viewController1, viewController2]; 

    // Setup the mirroring of the UI state; when Firebase lets us know the value has been updated, reflect that in the UI 
    Firebase* f = [[[Firebase alloc] initWithUrl:kFirebase] childByAppendingPath:@"selectedIndex"]; 
    [f observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) { 
     self.tabBarController.selectedIndex = [snapshot.value intValue]; 
    }]; 

    self.window.rootViewController = self.tabBarController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 
    NSUInteger newIndex = [((UITabBar *)object).items indexOfObject:change[NSKeyValueChangeNewKey]]; 

    // We've been notified via KVO that the UI has changed, so save the new state to Firebase 
    [[[[Firebase alloc] initWithUrl:kFirebase] childByAppendingPath:@"selectedIndex"] setValue:[NSNumber numberWithInt:newIndex]]; 
} 


@end 

有些事情解決接口鏡像時要記住:假設一臺設備上的用戶做一個方向開關,你就能夠很容易地保存關閉這對火力地堡,但將不得不作出應用程序特定的鏡像翻譯(如果其他設備選擇不旋轉他們的設備?)。

+0

我已經建立了你上面的例子和它的作品!不過,我仍然無法使用Firebase和UISWipeGesture或UIBUtton。我曾嘗試使用.selected(self.viewButton1.selected = [snapshot.value的intValue],但它不是在用戶界面中反映observeValueForKeyPath: - (無效)observeValueForKeyPath:(的NSString *)的keyPath ofObject:(ID)對象如果(object == self.view1Button1 && [keyPath isEqualToString:@「selected」]) {[[Firebase alloc] initWithUrl:kFirebase] childByAppendingPath :@「selectedButtonView1」]的setValue:@「1」] – Doug 2013-04-14 14:11:03

+0

疑難雜症,讓我踢周圍的一些代碼,我會回來給你一個完整的例子,敬請期待! – Vikrum 2013-04-16 18:38:10