2015-04-20 96 views
1

我一直在努力過去一段時間,應該很容易,但我似乎無法開始工作。我在WatchKit界面上綁定了一個現有iPhone應用程序,該應用程序在Objective-C開發的App Store中銷售。我只是想在將對象傳遞給下一個WKInterfaceController時,我滑動以繼續播放它。在基於頁面的WatchKit應用程序中的WKInterfaceControllers之間傳遞對象

鑑於WatchKit沒有prepareForSegue,並且基於頁面的關係分段沒有可以使用contextForSegueWithIdentifier的標識符(我可以告訴),我該如何去傳遞一個對象?

我曾嘗試:

_names = [[NSArray alloc]initWithObjects:@"RootInterfaceController",@"StatusInterfaceController",nil]; 
NSString *[email protected]"TEST"; 
    _contextObject=[[NSArray alloc]initWithObjects:passedBAL, nil]; 
    if (firstLoad) { 
     [WKInterfaceController reloadRootControllersWithNames:_names 
                contexts:_contextObject]; 
     firstLoad=NO; 

-(void)willActivate,在那裏我已經在-(void)awakeWithContext:(id)context設定的數值作爲_passedBal財產和BOOL避免無限遞歸這種方法似乎產生了(很缺憾修復在我看來),但是當我到達下一個WKInterfaceController時,價值總是零。當我在StatusInterfaceController的變量上設置斷點和鼠標時,它似乎設置了該值,但是隨着程序繼續執行,它立即刪除,但不響應我在代碼中寫入的任何內容。

任何幫助將不勝感激,因爲我一直在這一個拉我的頭髮,正如我所說,它應該死了簡單。

編輯:

_passedBAL是一個簡單的NSString財產,但我已經在上面的改變它只是一個直線上升的NSString把數組中爲清楚起見,並加入我的StatusInterfaceController代碼。感謝提前:)

在我StatusInterfaceController.h,我有以下幾點:

#import <WatchKit/WatchKit.h> 
#import <Foundation/Foundation.h> 
#import "InterfaceController.h" 
@interface StatusInterfaceController : WKInterfaceController 
@property (strong, nonatomic) NSString *passedBAL; 

@end 

雖然我StatusInterfaceController.m我:

#import "StatusInterfaceController.h" 

@interface StatusInterfaceController() 
@end 

@implementation StatusInterfaceController 

- (void)awakeWithContext:context { 
    [super awakeWithContext:context]; 
    _passedBAL=[context objectAtIndex:0]; 
    NSLog(@"Passed BAL is equal to %@",_passedBAL); 
    // Configure interface objects here. 
} 

回答

4

你可以做這有兩種方法:

在你的故事板中,你在你的segue中設置了一個標識符:

enter image description here

,然後你可以使用contextForSegueWithIdentifier

- (id)contextForSegueWithIdentifier:(NSString *)segueIdentifier { 
    if ([segueIdentifier isEqualToString:@"yourIdentifier"]) { 
     return aDictionaryWithYourInformation; 
    } 
} 

或者你也可以通過代碼的上下文信息傳遞,具有:

[self pushControllerWithName:@"YourViewController" 
        context:aDictionary]; 

這方面是一本字典,你請訪問本字典中的- (void)awakeWithContext:(id)context

編輯

現在我們可以看到更多的代碼,在我看來,在reload和awakeWithContext中存在一些混淆。

在重新加載方法中,您傳遞一組上下文,基本上每個接口的上下文都是。然後,你會在你的awakeWithContext中收到一個單一的上下文,在你的情況下是你想要的字符串。因此,您在根接口控制器 awakeWithContext應該是上下文:

- (void)awakeWithContext:context { 
    [super awakeWithContext:context]; 
    NSLog(@"Passed BAL is equal to %@",context); 
} 

這種方法將是零在StatusInterfaceController因爲你沒有傳遞任何情況下它。

+0

謝謝,但從我所知道的,你不能設置關係Segue(基於頁面的導航)的標識符。當您突出顯示時,屬性菜單下沒有可用的選項。在你的第二個解決方案中,我需要創建一個按鈕來繼續,當我只想要通過滑動傳遞的信息(顯然,它也不能作爲接口對象添加到WatchKit中)。任何想法,我會如何做到這一點? –

+0

對不起,您不明白您正在談論基於頁面的導航(請在您的問題中明確說明以縮小將來的答案)。據我所知,最好的方法是用不同的上下文重新加載你的視圖控制器,看起來你已經在做這件事了。 –

+0

感謝蒂亞戈,也許我的代碼是在接收端扭曲...那麼在我接收WKViewController,我有 - (void)awakeWithContext:context {superwakeWithContext:context]; _passedBAL = [context objectAtIndex:0]; (@「通過的BAL等於%@」,_ passedBAL);如果我有一個屬性設置,並且我將這個值傳遞給一個NSArray,我選擇了第一個對象。那是不正確的? –

相關問題