2

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: enter image description here

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:

enter image description here

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

    .
    .
    .
    .
39fredy
  • 1,923
  • 2
  • 21
  • 40

1 Answers1

2

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()
}
 } 
E.Coms
  • 11,065
  • 2
  • 23
  • 35
  • 1
    I'm trying to do the following: Navigate from the Main `HostingController` to HC3 using `NavigationLink` then have the page based navigation between HC2, HC3 and HC4. If I put the `reloadRootPageController` in `class HostingController: WKHostingController` then I go directly to page based navigation and skip seeing the `ContentView()`. I'm trying to replicate the navigation from the native workout app. Where you pick a work out and then have 3 pages to navigate from (the main page, the settings page and the music page etc) – 39fredy Oct 25 '19 at 03:59
  • Wouldn't the user pressing the button and navigating to HC3 are that the top WKHostingController ? Thanks in advance ! – 39fredy Oct 25 '19 at 04:05
  • I update the answer. You still can use HC3 if the loadingRootPage only run once after awake. Otherwise, the HC3 will keep loading RootPage every time the HC3 is loaded. – E.Coms Oct 25 '19 at 15:30
  • @E.Coms When I use the first approach suggested in your answer the ContentView of the HostingController is ignored and the user is taken straight away to the page based navigation. If I remove the reloadRootPageControllers call from HostingController I see the ContentView, but upon navigation to the Page view I only see the page I navigate to and not the two others. – Nikolai Hegelstad Apr 05 '20 at 17:04
  • Solution was to call reloadRootPageControllers from the HC3 with the static bool runOnce. A little dirty but it works well. – Nikolai Hegelstad Apr 06 '20 at 06:33