简体   繁体   English

在无法在Apple Watch中运行的接口控制器之间传递数据

[英]passing data between interfacecontrollers not working in apple watch

这就是我拥有视图控制器的方式

I was using this code to pass data between controllers. 我正在使用此代码在控制器之间传递数据。

InterfaceController.reloadRootControllersWithNames(["1","2","3"], contexts: ["adf","asd","asd"])

I called this code in willActivate() method, that you can see here. 我在willActivate()方法中调用了此代码,您可以在此处看到。

 override func willActivate() {
        // This method is called when watch view controller is about to be visible to user
        super.willActivate()
        InterfaceController.reloadRootControllersWithNames(["1","2","3"], contexts: ["adf","asd","asd"])
    }

I have no idea about whats wrong with this code, in apple watch, the app is just refreshing again and again. 我不知道这段代码有什么问题,在苹果手表中,该应用程序一次又一次地刷新。 Is there any other method to pass data between interface controllers while we use page based navigation.?? 使用基于页面的导航时,还有其他方法可以在接口控制器之间传递数据吗?

Please Find the below way of passing data in paged-based navigation in watch application (Watch os 2 and later). 请在监视应用程序(监视操作系统2和更高版本)中找到以下在基于页面的导航中传递数据的方式。

Note : 注意 :

An array of strings, each of which contains the name of an interface controller you want to display in the page-based interface. 字符串数组,每个字符串包含要在基于页面的界面中显示的界面控制器的名称。 In your storyboard, the name of an interface controller is stored in the object's Identifier property, which is located in the attributes inspector. 在情节提要中,接口控制器的名称存储在对象的Identifier属性中,该属性位于属性检查器中。 The order of the strings in the array is used to set the order of the corresponding interface controllers. 数组中字符串的顺序用于设置相应接口控制器的顺序。 This parameter must not be nil or an empty array. 此参数不能为nil或为空数组。

Objectiv C 目标C

[self presentControllerWithNames:@[@"FirstViewController",@"SecondViewController",@"ThirdViewController"] contexts:@[@"adf",@"asd",@"asd"]];

Swift 迅速

let controller = ["FirstViewController","SecondViewController","ThirdViewController"]
    let pageContexts:[AnyObject]? = ["adf","asd","asd"]

    presentControllerWithNames(controller, contexts: pageContexts)

If you call reloadRootControllersWithNames(NSArray, contexts: NSArray) onto the view controller, it will do as the function is titled: reload. 如果将reloadRootControllersWithNames(NSArray, contexts: NSArray)调用到视图控制器上,它将按标题为reload.的函数进行操作reload. Therefore, you have a view controller that refreshes all 3 of the items in the first Array you give, and then, since your current view controller calls willActivate() upon loading, it infinitely refreshes. 因此,您有一个视图控制器可以刷新您给定的第一个数组中的所有3个项目,然后,由于当前的view controller在加载时会调用willActivate() ,因此它将无限刷新。

I don't have enough reputation to make this a comment, but my suggestion here is to find a method that you can call on an instance of the watch interface you want to in order to pass data. 我没有足够的声誉来发表评论,但是我的建议是找到一种方法,您可以在想要的watch接口实例上调用该方法以传递数据。

What I mean is, if you have each interface as its own class, then you can make a method in the interface you want to get to that sets a property to whatever data you want to transfer before you present the interface. 我的意思是,如果您将每个接口作为自己的类,则可以在要提供的接口中创建一个方法,以在显示接口之前将属性设置为要传输的任何数据。

I also use Objective-C more than Swift, but here's what my guess is as to what the Obj-C code would come to: 与Swift相比,我还更多地使用了Objective-C,但这是我对Obj-C代码的理解:

SecondWatchInterface *secondWatchInterface = [SecondWatchInterface new];
[SecondWatchInterface setSomeDataWithAStringValue:@"Foo"];
[self presentWatchInterface:SecondWatchInterface]

In Swift, I'm thinking this would equate to: 在Swift中,我认为这等同于:

SecondWatchInterface *secondWatchInterface = SecondWatchInterface.new()
secondWatchInterface.setSomeDataWithAStringFile:"Foo"
presentWatchInterface(secondWatchInterface)

I'm unfamiliar with WatchKit myself; 我本人不熟悉WatchKit; I typically use SpriteKit. 我通常使用SpriteKit。 I hope I helped identify what the problem was, at least! 我希望至少可以帮助我找出问题所在!

To pass data between interface controllers you need to go for this method: 要在接口控制器之间传递数据,您需要使用以下方法:

presentControllerWithName(“Second Watch Controller”, context: [“segue”:“pagebased”,“data”: "Data Passed Successfully”]);

Also, you can refer to this example: http://www.informit.com/articles/article.aspx?p=2272657&seqNum=3 另外,您可以参考以下示例: http : //www.informit.com/articles/article.aspx?p=2272657&seqNum=3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM