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

我只是一个虾纸丫 提交于 2021-01-27 20:08:50

问题


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

    .
    .
    .
    .

回答1:


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


来源:https://stackoverflow.com/questions/58546593/issues-implementing-navigation-from-a-main-controller-to-page-based-controllers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!