使用 SwiftUI 在 WatchOS 中实现从主控制器到基于页面的控制器的导航的问题 [英] Issues implementing navigation from a main controller to page based controllers in WatchOS using SwiftUI

查看:38
本文介绍了使用 SwiftUI 在 WatchOS 中实现从主控制器到基于页面的控制器的导航的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做类似

如您所见,故事板中没有任何 segues下一页关系.

我正在 HC3WKHostingController(三者中的中间一个)中的代码中实现这些.

HC3

HostingController:

class HC3:WKHostingController{覆盖 func 唤醒(带上下文上下文:任何?){super.awake(withContext: 上下文)WKInterfaceController.reloadRootPageControllers(withNames: ["HC2", "HC3", "HC4"], 上下文: [context] , 方向: WKPageOrientation.horizo​​ntal, pageIndex: 1)}覆盖 var 主体:CV {返回简历()}}

问题是我无法在基于页面的视图之间导航.

其他 HostingController 有一个 WKHostingController 类型的类,如下所示:

class HC[#]: WKHostingController{覆盖 var 主体:CV {返回简历()}}

他们在身份检查器中分配了类,并且在属性检查器中也有指定的 ID.

我使用 NavigationLink 从主控制器导航到基于分页的控制器,这是视图或主托管控制器:

struct ContentView: 查看 {var主体:一些视图{NavigationLink(目的地名称:HC3"){Text("转到 HC3")}}}

示例:

尝试导航到基于页面的控制器中的其他页面时,我确实在控制台中遇到了一些错误:

ComF:interfaceControllerID:13F0353 的 interfaceController 未找到 (clientIdentifier=(null))SampleApp WatchKit Extension[319:69539] [default] -[SPRemoteInterface _interfaceControllerClientIDForControllerID:]:2358: ComF: 找不到 interfaceControllerID 的 clientIdentifier:13F0353.调用堆栈:(0 手表套件 0x36dd72fc 0045BA6A-0953-3B1D-915F-6ADB695CD163 + 1727961 手表套件 0x36dd90cc 0045BA6A-0953-3B1D-915F-6ADB695CD163 + 1804282 WatchKit 0x36dcf2cc spUtils_dispatchAsyncToMainThread + 403 手表套件 0x36dd8db8 0045BA6A-0953-3B1D-915F-6ADB695CD163 + 1796404 WatchKit 0x36dcf2cc spUtils_dispatchAsyncToMainThread + 405 手表套件 0x36dd6688 0045BA6A-0953-3B1D-915F-6ADB695CD163 + 1696086 手表套件 0x36dd6564 0045BA6A-0953-3B1D-915F-6ADB695CD163 + 1693167 手表套件 0x36dcd9f4 0045BA6A-0953-3B1D-915F-6ADB695CD163 + 1336208 手表套件 0x36dd632c 0045BA6A-0953-3B1D-915F-6ADB695CD163 + 1687489 手表套件 0x36dd623c 0045BA6A-0953-3B1D-915F-6ADB695CD163 + 16850810 手表套件 0x36db0b74 0045BA6A-0953-3B1D-915F-6ADB695CD163 + 1522011 手表套件 0x36dbae94 0045BA6A-0953-3B1D-915F-6ADB695CD163 + 5698012 UIKitCore 0x4148aaf0 78873E50-5E9B-3AA3-A471-366668659CA2 + 923518413 UIKitCore 0x40d750ec 78873E50-5E9B-3AA3-A471-366668659CA2 + 180657214 UIKitCore 0x40d75454 78873E50-5E9B-3AA3-A471-366668659CA2 + 180744415 UIKitCore 0x40d74cf0 78873E50-5E9B-3AA3-A471-366668659CA2 + 180555216 UIKitCore 0x40d7934c 78873E50-5E9B-3AA3-A471-366668659CA2 + 182356417 UIKitCore 0x410af7f0 78873E50-5E9B-3AA3-A471-366668659CA2 + 519166418 UIKitCore 0x41185800 _UISceneSettingsDiffActionPerformChangesWithTransitionContext + 244....

解决方案

您的设计存在逻辑错误.reloadRootPageController 函数应该在顶部 WKHostingController 中调用,但HC3".

 class HostingController: WKHostingController{覆盖 func 唤醒(带上下文上下文:任何?){super.awake(withContext: 上下文)WKInterfaceController.reloadRootPageControllers(withNames: ["HC2", "HC3", "HC4"], 上下文: [context] , 方向: WKPageOrientation.horizo​​ntal, pageIndex: 1)}覆盖 var 主体:ContentView {返回内容视图()}}

如果reloadRootPageControllers在HC3中被调用,那么奇怪的情况就是你遇到的.

否则,您必须在 HC3 设置中添加 conditional_once.

class HC3:WKHostingController{静态变量 runOnce: Bool = true覆盖 func 唤醒(带上下文上下文:任何?){super.awake(withContext: 上下文)如果 HC3.runOnce { HC3.runOnce.toggle()WKInterfaceController.reloadRootPageControllers(withNames: ["HC2", "HC3", "HC4"], 上下文: [context] , 方向: WKPageOrientation.horizo​​ntal, pageIndex: 1)}}覆盖 var 主体:CV {返回简历()}}

I'm trying to do do something like this using SwiftUI. So far I have the ability to go from one main view to a page based views but I cannot scroll between the page views.

The storyboard looks like this:

As you can see I do not have any segues or next page relationships in the storyboard.

I'm implementing those in code in the WKHostingController of HC3 (the middle one of the three).

HostingController of HC3:

class HC3: WKHostingController<CV> {

    override func awake(withContext context: Any?) {
        super.awake(withContext: context)
        WKInterfaceController.reloadRootPageControllers(withNames: ["HC2", "HC3", "HC4"], contexts: [context] , orientation: WKPageOrientation.horizontal, pageIndex: 1)
    }
    override var body: CV {
        return CV()
    }
}

The issue is that I cannot navigate between the page based views.

The other HostingControllers have a a class of type WKHostingController as follows:

class HC[#]: WKHostingController<CV> {
    override var body: CV {
        return CV()
    }
}

They have the classed assigned in the Identity inspector and they also have the specified ID in the Attributes inspector.

Im navigating from the main controller to the paged based controllers by using a NavigationLink Here is the View or the main hosting controller:

struct ContentView: View {
    var body: some View {
        NavigationLink(destinationName: "HC3"){
            Text("Go to HC3")
        }
    }
}

Example:

I do get some errors in the console when trying to navigate to other pages in the page based controller:

ComF: interfaceController for interfaceControllerID:13F0353 not found (clientIdentifier=(null))


SampleApp WatchKit Extension[319:69539] [default] -[SPRemoteInterface _interfaceControllerClientIDForControllerID:]:2358: ComF: clientIdentifier for interfaceControllerID:13F0353 not found. callStack:(
    0   WatchKit                            0x36dd72fc 0045BA6A-0953-3B1D-915F-6ADB695CD163 + 172796
    1   WatchKit                            0x36dd90cc 0045BA6A-0953-3B1D-915F-6ADB695CD163 + 180428
    2   WatchKit                            0x36dcf2cc spUtils_dispatchAsyncToMainThread + 40
    3   WatchKit                            0x36dd8db8 0045BA6A-0953-3B1D-915F-6ADB695CD163 + 179640
    4   WatchKit                            0x36dcf2cc spUtils_dispatchAsyncToMainThread + 40
    5   WatchKit                            0x36dd6688 0045BA6A-0953-3B1D-915F-6ADB695CD163 + 169608
    6   WatchKit                            0x36dd6564 0045BA6A-0953-3B1D-915F-6ADB695CD163 + 169316
    7   WatchKit                            0x36dcd9f4 0045BA6A-0953-3B1D-915F-6ADB695CD163 + 133620
    8   WatchKit                            0x36dd632c 0045BA6A-0953-3B1D-915F-6ADB695CD163 + 168748
    9   WatchKit                            0x36dd623c 0045BA6A-0953-3B1D-915F-6ADB695CD163 + 168508
    10  WatchKit                            0x36db0b74 0045BA6A-0953-3B1D-915F-6ADB695CD163 + 15220
    11  WatchKit                            0x36dbae94 0045BA6A-0953-3B1D-915F-6ADB695CD163 + 56980
    12  UIKitCore                           0x4148aaf0 78873E50-5E9B-3AA3-A471-366668659CA2 + 9235184
    13  UIKitCore                           0x40d750ec 78873E50-5E9B-3AA3-A471-366668659CA2 + 1806572
    14  UIKitCore                           0x40d75454 78873E50-5E9B-3AA3-A471-366668659CA2 + 1807444
    15  UIKitCore                           0x40d74cf0 78873E50-5E9B-3AA3-A471-366668659CA2 + 1805552
    16  UIKitCore                           0x40d7934c 78873E50-5E9B-3AA3-A471-366668659CA2 + 1823564
    17  UIKitCore                           0x410af7f0 78873E50-5E9B-3AA3-A471-366668659CA2 + 5191664
    18  UIKitCore                           0x41185800 _UISceneSettingsDiffActionPerformChangesWithTransitionContext + 244

    .
    .
    .
    .

解决方案

There is a logic error in your design. The reloadRootPageController function should be called in the top WKHostingController but "HC3".

 class HostingController: WKHostingController<ContentView> {

override func awake(withContext context: Any?) {
       super.awake(withContext: context)
       WKInterfaceController.reloadRootPageControllers(withNames: ["HC2", "HC3", "HC4"], contexts: [context] , orientation: WKPageOrientation.horizontal, pageIndex: 1)
   }


override var body: ContentView {
    return ContentView()
}
}

If reloadRootPageControllers is called in HC3, the strange situation is what you met.

Otherwise, you have to add a conditional_once in your HC3 setting.

class HC3: WKHostingController<CV> {

 static var runOnce: Bool = true

 override func awake(withContext context: Any?) {
    super.awake(withContext: context)

    if HC3.runOnce { HC3.runOnce.toggle()
     WKInterfaceController.reloadRootPageControllers(withNames: ["HC2", "HC3", "HC4"], contexts: [context] , orientation: WKPageOrientation.horizontal, pageIndex: 1)
    }

  }

override var body: CV {
    return CV()
}
 } 

这篇关于使用 SwiftUI 在 WatchOS 中实现从主控制器到基于页面的控制器的导航的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆