如何将数据从一个 WKInterfaceController 推送到另一个?

How to Push Data From one WKInterfaceController to Another?

我的 WatchKit 中有 2 个接口控制器。第一个称为 InterfaceController,而另一个称为 DetailsForWatch。 IC 上有一个 tableView。它从 Parse class 解析数据,并将 class 中每个条目的数据显示为一行。这很好用。

我想做的是将 selected 行的 PFObject 传递给 DetailsForWatch 中的 PFObject。我的 DFW 设置是:

.h

@interface DetailsForWatch : WKInterfaceController {
}
@property (nonatomic, retain) IBOutlet WKInterfaceLabel *detailsLabel;
@property (nonatomic, retain) PFObject *finalObject;

@end

.m

- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];
    NSString *details = self.finalObject [@"Request"];

    [self.detailsLabel setText:details];
    NSLog(@"%@", self.finalObject);
    // Configure interface objects here.
}

在 IC 中,对于 .h 我有:

@class DetailsForWatch;
@interface InterfaceController : WKInterfaceController {
    DetailsForWatch *_theDetails;


}
@property (retain) DetailsForWatch *theDetails;
@end

在 .m 中我有:

@synthesize theDetails = _theDetails;

对于 didSelectRowAtIndex,我有:

_theObject = _theObjective[rowIndex];

    self.theDetails = [[DetailsForWatch alloc] init];
    _theDetails.finalObject = _theObject;

我将 DFW 设置为来自 IC 组的推送 selection。当我在 IC 中 select 一行时,它会推送一个空白屏幕,并且 NSLog 显示名为 finalObject 的 PFObject 是 (null)。它没有正确传递 PFObject,我做错了什么?

有几种方法可以在两个接口控制器之间传递数据。我一直这样做的方式是这样的:

  1. 在我的故事板中的两个控制器之间创建一个 segue(必要时给它一个标识符)。

  2. 在接口控制器1中实现

- (id)contextForSegueWithIdentifier:(NSString *)segueIdentifier

当通过按下按钮或其他方式触发 segue 时将调用它。

这可以 return 任何对象,例如数据字典(在您的例子中 'theDetails')

  1. 在接口控制器2中实现

- (void)awakeWithContext:(id)context

此处的上下文对象将是您在控制器 1 中传递的对象