在基于页面的 WatchKit 应用程序中的 WKInterfaceControllers 之间传递对象
Pass object between WKInterfaceControllers in Page-Based WatchKit App
在过去的一段时间里,我一直在为一些本应非常容易的事情而苦苦挣扎,但我似乎无法开始工作。我正在将 WatchKit 接口连接到我在 App Store 中出售的现有 iPhone 应用程序,该应用程序是在 Objective-C 中开发的。我只是想在滑动时将一个对象传递给下一个 WKInterfaceController
。
鉴于 WatchKit 没有 prepareForSegue,并且基于页面的关系 Segues 没有标识符(据我所知)这将使我能够使用 contextForSegueWithIdentifier
,我应该如何传递一个对象?
我试过:
_names = [[NSArray alloc]initWithObjects:@"RootInterfaceController",@"StatusInterfaceController",nil];
NSString *passedBAL=@"TEST";
_contextObject=[[NSArray alloc]initWithObjects:passedBAL, nil];
if (firstLoad) {
[WKInterfaceController reloadRootControllersWithNames:_names
contexts:_contextObject];
firstLoad=NO;
in -(void)willActivate
,我在 -(void)awakeWithContext:(id)context
中为 _passedBal
属性 和 BOOL 设置了一个值为了避免这种方法似乎创建的无限递归(在我看来非常笨拙的修复),但是当我到达下一个 WKInterfaceController
时,该值始终为 nil。当我设置断点并将鼠标悬停在 StatusInterfaceController
中的变量上时,它似乎设置了该值,但随着程序继续执行,它立即将其置零,尽管这不是对我在代码中编写的任何内容的响应.
任何帮助将不胜感激,因为我一直在为这个问题努力,正如我所说,它应该非常简单。
EDIT:
_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.
}
您可以通过两种方式做到这一点:
在你的故事板中,你在你的 segue 中设置了一个标识符:
然后你可以使用contextForSegueWithIdentifier
:
- (id)contextForSegueWithIdentifier:(NSString *)segueIdentifier {
if ([segueIdentifier isEqualToString:@"yourIdentifier"]) {
return aDictionaryWithYourInformation;
}
}
或者您可以通过代码传递带有上下文的信息,使用:
[self pushControllerWithName:@"YourViewController"
context:aDictionary];
此上下文是一个字典,您可以在 - (void)awakeWithContext:(id)context
中访问此字典
编辑
现在我们可以看到更多代码,在我看来,您的 reload 和 awakeWithContext 有点混乱。
在重新加载方法中,您传递了一组上下文,这基本上是每个接口的上下文。然后,您在 awakeWithContext 中收到一个上下文,在您的情况下就是您想要的字符串。因此,你的 Root Interface Controller 中的 awakeWithContext 应该是上下文:
- (void)awakeWithContext:context {
[super awakeWithContext:context];
NSLog(@"Passed BAL is equal to %@",context);
}
这个方法在您的 StatusInterfaceController 中将为 nil,因为您没有向它传递任何上下文。
在过去的一段时间里,我一直在为一些本应非常容易的事情而苦苦挣扎,但我似乎无法开始工作。我正在将 WatchKit 接口连接到我在 App Store 中出售的现有 iPhone 应用程序,该应用程序是在 Objective-C 中开发的。我只是想在滑动时将一个对象传递给下一个 WKInterfaceController
。
鉴于 WatchKit 没有 prepareForSegue,并且基于页面的关系 Segues 没有标识符(据我所知)这将使我能够使用 contextForSegueWithIdentifier
,我应该如何传递一个对象?
我试过:
_names = [[NSArray alloc]initWithObjects:@"RootInterfaceController",@"StatusInterfaceController",nil];
NSString *passedBAL=@"TEST";
_contextObject=[[NSArray alloc]initWithObjects:passedBAL, nil];
if (firstLoad) {
[WKInterfaceController reloadRootControllersWithNames:_names
contexts:_contextObject];
firstLoad=NO;
in -(void)willActivate
,我在 -(void)awakeWithContext:(id)context
中为 _passedBal
属性 和 BOOL 设置了一个值为了避免这种方法似乎创建的无限递归(在我看来非常笨拙的修复),但是当我到达下一个 WKInterfaceController
时,该值始终为 nil。当我设置断点并将鼠标悬停在 StatusInterfaceController
中的变量上时,它似乎设置了该值,但随着程序继续执行,它立即将其置零,尽管这不是对我在代码中编写的任何内容的响应.
任何帮助将不胜感激,因为我一直在为这个问题努力,正如我所说,它应该非常简单。
EDIT:
_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.
}
您可以通过两种方式做到这一点:
在你的故事板中,你在你的 segue 中设置了一个标识符:
然后你可以使用contextForSegueWithIdentifier
:
- (id)contextForSegueWithIdentifier:(NSString *)segueIdentifier {
if ([segueIdentifier isEqualToString:@"yourIdentifier"]) {
return aDictionaryWithYourInformation;
}
}
或者您可以通过代码传递带有上下文的信息,使用:
[self pushControllerWithName:@"YourViewController"
context:aDictionary];
此上下文是一个字典,您可以在 - (void)awakeWithContext:(id)context
编辑
现在我们可以看到更多代码,在我看来,您的 reload 和 awakeWithContext 有点混乱。
在重新加载方法中,您传递了一组上下文,这基本上是每个接口的上下文。然后,您在 awakeWithContext 中收到一个上下文,在您的情况下就是您想要的字符串。因此,你的 Root Interface Controller 中的 awakeWithContext 应该是上下文:
- (void)awakeWithContext:context {
[super awakeWithContext:context];
NSLog(@"Passed BAL is equal to %@",context);
}
这个方法在您的 StatusInterfaceController 中将为 nil,因为您没有向它传递任何上下文。