简体   繁体   English

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

[英]Issues implementing navigation from a main controller to page based controllers in WatchOS using SwiftUI

I'm trying to do do something like this using SwiftUI.我正在尝试使用 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.到目前为止,我有能力将 go 从一个主视图切换到基于页面的视图,但我无法在页面视图之间滚动。

The storyboard looks like this: storyboard 看起来像这样: 在此处输入图像描述

As you can see I do not have any segues or next page relationships in the storyboard.如您所见,我在segues中没有任何转场或next page relationships

I'm implementing those in code in the WKHostingController of HC3 (the middle one of the three).我在HC3WKHostingController (三者的中间之一)中的代码中实现了这些。

HostingController of HC3 : HC3HostingController

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 aa class of type WKHostingController as follows:其他 HostingControllers 有一个 WKHostingController 类型的WKHostingController如下:

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.它们在身份检查器中分配了类别,并且在属性检查器中也具有指定的 ID。

Im navigating from the main controller to the paged based controllers by using a NavigationLink Here is the View or the main hosting controller:我使用NavigationLink从主 controller 导航到基于分页的控制器这是视图或主托管 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:尝试导航到基于 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". reloadRootPageController function 应在顶部 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()
}
}

If reloadRootPageControllers is called in HC3, the strange situation is what you met.如果在HC3中调用reloadRootPageControllers ,那么奇怪的情况就是你遇到的。

Otherwise, you have to add a conditional_once in your HC3 setting.否则,您必须在 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()
}
 } 

暂无
暂无

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

相关问题 WatchOS 上的 SwiftUI 应用程序中是否可以进行基于页面的导航? - Is page-based navigation possible in a SwiftUI app on WatchOS? 在 watchOS 上使用 SwiftUI,如何将视图与导航标题对齐? - Using SwiftUI on watchOS, how to align views to the navigation title? 如何使用现有导航 controller 从 swiftui 导航到视图控制器 - how to navigate from swiftui to viewcontroller using existing navigation controller 在视图控制器之间传递数据使用从导航控制器中嵌入的视图到tabbarcontroller的segue - Passing Data between view Controllers Using a segue from a view embedded in a navigation controller to a tabbarcontroller 从Xcode的主控制器中删除导航栏? - Remove navigation bar from main controller in Xcode? 如何从 WatchOS 的 SwiftUI 列表中删除分隔符? - How to remove separators from SwiftUI list in WatchOS? 停止导航控制器影响其他视图控制器 - Stop Navigation Controller from affecting other View Controllers Landmarks SwiftUI 教程 - 创建 watchOS 应用程序 - 导航损坏 - Landmarks SwiftUI Tutorial - Creating watchOS App - Navigation Broken Apple Watch 上的内置锻炼应用程序如何从主表格行导航到一组基于页面的控制器? - How does the inbuilt Workout app on Apple Watch navigate from main table row to a set of page-based controllers? 当控制器不是当前页面运行但不更新任何UI时,WatchOS2 UI更新发送到基于页面的WKInterfaceController - WatchOS2 UI updates sent to page based WKInterfaceController when controller is not current page runs but does not update any UIs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM