2015-04-20 50 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,我选择了第一个对象。那是不正确的? –

相关问题