火力地堡沒有做特殊應用同步等接口鏡像任何特定的原語。您需要做的是將界面的當前狀態模型化爲Firebase中的JSON。因此,隨着用戶界面部分的更改,您可以將它們保存到Firebase中。然後,您還需要設置補充:當由於Firebase更改而觸發事件時,請更新UI。對於諸如手勢和動作之類的東西,需要使用相同類型的建模 - 將這些項目保存在Firebase中(或者,如果這樣做不能在語義上輕鬆轉換,請保留動作的副作用;例如,如果滑動導致按鈕移動位置,保存火力地堡按鈕的位置。)
具體而言,這裏是保持兩個選項卡同步的例子。這是基於默認的Xcode項目模板「Tabbled應用程序」。在這裏面我們做了幾件事情:
- 添加志願觀察員當標籤項目改變
- 安裝鏡像的變化反應從火力地堡
- 實際保存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
有些事情解決接口鏡像時要記住:假設一臺設備上的用戶做一個方向開關,你就能夠很容易地保存關閉這對火力地堡,但將不得不作出應用程序特定的鏡像翻譯(如果其他設備選擇不旋轉他們的設備?)。
我已經建立了你上面的例子和它的作品!不過,我仍然無法使用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
疑難雜症,讓我踢周圍的一些代碼,我會回來給你一個完整的例子,敬請期待! – Vikrum 2013-04-16 18:38:10