2015-06-19 35 views
0

我不熟悉在Apple Watch中的兩個WKInterfaceController之間傳輸或傳遞數據。我想要做的是,我有一些像nameageSecondInterfaceController這樣的變量,所以當用戶點擊一行時,我需要從WKInterfaceTable傳遞一些值給他們。這裏是代碼:將數據從WKInterfaceTable傳輸到另一個WKInterfaceController

- (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex { 

    NSString *name; 
    NSString *age; 

    switch (rowIndex) { 
     case 0: 
      name = @"Jack"; 
      age = @"22"; 
      break; 

     default: 
      break; 
    } 

    NSDictionary *d = [NSDictionary dictionaryWithObject:name forKey:@"kName"]; 
    [self pushControllerWithName:@"SecondInterfaceController" context:d]; 

} 

,但我不知道我怎樣才能獲得從SecondIntefaceController字典,並把它傳遞給_name(WKInterfaceLable)。

回答

1

當你推動你的SecondInterfaceController時,它將調用awakeWithContext:context參數將成爲你傳遞的字典。然後,您可以將名稱值從上下文字典中提取出來,並將其分配給您的標籤。

- (void)awakeWithContext:(id)context { 
    NSDictionary *dict = (NSDictionary *)context; 
    [_name setText:dict[@"kName"]]; 
} 

您可以通過向字典中添加多個鍵和值來傳遞多個值。

NSDictionary *d = @{@"kName" : name, @"kAge" : age}; 
[self pushControllerWithName:@"SecondInterfaceController" context:d]; 
+0

謝謝!但通過不同的字典呢!如果添加'dinoDict = [NSDictionary dictionaryWithObject:age forKey:@「kAge」];'只有一個字典會被傳遞,我怎樣才能將多個值附加到一個上下文中? –

+1

@ Mc.Lover我在我的答案中添加了一些代碼,以顯示如何在字典中傳遞多個值 – dan

相關問題