【问题标题】:passing data between interfacecontrollers not working in apple watch在接口控制器之间传递数据在 Apple Watch 中不起作用
【发布时间】:2015-04-17 06:14:58
【问题描述】:

我使用此代码在控制器之间传递数据。

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

我在 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"])
    }

我不知道这段代码有什么问题,在 Apple Watch 中,应用程序只是一次又一次地刷新。当我们使用基于页面的导航时,还有其他方法可以在界面控制器之间传递数据吗??

【问题讨论】:

    标签: ios iphone watchkit apple-watch


    【解决方案1】:

    请在手表应用程序(Watch os 2 及更高版本)的基于分页的导航中找到以下传递数据的方法。

    注意:

    字符串数组,每个字符串都包含您要在基于页面的界面中显示的界面控制器的名称。在故事板中,接口控制器的名称存储在对象的 Identifier 属性中,该属性位于属性检查器中。数组中字符串的顺序用于设置相应接口控制器的顺序。此参数不能为 nil 或空数组。

    目标 C

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

    斯威夫特

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

    【讨论】:

      【解决方案2】:

      如果您在视图控制器上调用reloadRootControllersWithNames(NSArray, contexts: NSArray),它将按照函数的标题:reload. 因此,您有一个视图控制器,它刷新您提供的第一个数组中的所有 3 个项目,然后,由于您当前的view controller 在加载时调用willActivate(),因此它会无限刷新。

      我没有足够的声誉来发表评论,但我在这里的建议是找到一种方法,您可以调用您想要的手表接口实例以传递数据。

      我的意思是,如果您将每个接口都作为自己的类,那么您可以在接口中创建一个方法您想要访问,该方法将属性设置为您想要传输的任何数据在你展示界面之前。

      我也更多地使用 Objective-C 而不是 Swift,但以下是我对 Obj-C 代码的猜测:

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

      在 Swift 中,我认为这相当于:

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

      我自己对 WatchKit 不熟悉;我通常使用 SpriteKit。我希望我能帮助找出问题所在,至少!

      【讨论】:

        【解决方案3】:

        要在接口控制器之间传递数据,您需要使用此方法:

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

        另外,你可以参考这个例子:http://www.informit.com/articles/article.aspx?p=2272657&seqNum=3

        【讨论】:

          猜你喜欢
          • 2017-09-11
          • 1970-01-01
          • 1970-01-01
          • 2021-02-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多