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
How to Refresh Multiple Timers in Widget iOS14
If-Let Any to Rawrepresentable<String>
Swift 5, How to Execute Code After Fetching All Childadded from Firebase
Swift Lazy Stored Property Versus Regular Stored Property When Using Closure
Swift Firestore Search for Users
In Swift, How to Wait Until a Server Response Is Received Before I Proceed
Cast Any to Float Always Fails in Swift4.1
Explicitly Unwrapping Optional Nil Does Not Cause Crash
Is There *Any* Situation Under Which "For _ in [1,2,3]" Will Not Loop at All
Swift Error Using Initialized Properties in Expressions Before Super.Init
How to Calculate the Energy Per Bin in a Dft
How to Wait for Http Requests to Finish
Swift How to Convert Parse Createdat Time to Time Ago
Why Do We Need to Set Delegate to Self? Why Isn't It Defaulted by the Compiler
How to Convert Dispatchtimeinterval to Nstimeinterval (Or Double)