How Does the Inbuilt Workout App on Apple Watch Navigate from Main Table Row to a Set of Page-Based Controllers

How does the inbuilt Workout app on Apple Watch navigate from main table row to a set of page-based controllers?

OBJC:

+ (void)reloadRootPageControllersWithNames:(NSArray<NSString *> *)names 
contexts:(NSArray *)contexts
orientation:(WKPageOrientation)orientation
pageIndex:(NSInteger)pageIndex;

SWIFT:

class func reloadRootPageControllers(withNames names: [String], 
contexts: [Any]?,
orientation: WKPageOrientation,
pageIndex: Int)

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

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

Setting the initial view controller in a page based navigation to something other than the first view

You just have to insert in your second controller a call to becomeCurrentPage() in the awake method:

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

// ...

becomeCurrentPage()
}

How can I make a storyboard to the left of an initial view storyboard on Apple Watch using SwiftuI?

Well, in SwiftUI 2.0 it looks like there is a very easy way to integrate this with a simple TabView and I'll show an example

struct threeViewsInOne{

@State private var selectedPage = 1


var body: some View {
TabView(selection: $selectedPage) {
ViewA.tag(0)
ViewB.tag(1)
ViewC.tag(2)
}
.tabViewStyle(PageTabViewStyle())
}
}


Related Topics



Leave a reply



Submit