使用SwiftUI在WatchOS中实现从主控制器到基于页面的控制器的导航

问题描述 投票:0回答:1

我正在尝试使用SwiftUI做类似this的操作。到目前为止,我已经能够从一个主视图转到基于页面的视图,但是我无法在页面视图之间滚动。

情节提要看起来像这样:enter image description here

如您所见,情节提要中没有任何seguesnext page relationships

我正在WKHostingControllerHC3(三个中的一个)中的代码中实现它们。

HostingControllerHC3

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()
    }
}

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

其他HostingControllers具有如下类型的WKHostingController类:

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

它们在身份检查器中分配了类别,并且在属性检查器中也具有指定的ID。

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

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

示例:

enter image description here

当尝试导航到基于页面的控制器中的其他页面时,控制台中确实出现了一些[[错误:

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 . . . .
swift watchkit swiftui watch-os watch-os-6
1个回答
0
投票
您的设计中存在逻辑错误。 reloadRootPageController函数应在顶部WKHostingController中调用,但应为“ 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() } }

如果在HC3中调用了reloadRootPageControllers,则您遇到的是奇怪的情况。

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

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() } }

© www.soinside.com 2019 - 2024. All rights reserved.