简体   繁体   English

WatchOS 上的 SwiftUI 应用程序中是否可以进行基于页面的导航?

[英]Is page-based navigation possible in a SwiftUI app on WatchOS?

I see page-based navigation is supported for WatchOS, but is there a way to access this functionality with SwiftUI?我看到 WatchOS 支持基于页面的导航,但有没有办法使用 SwiftUI 访问此功能?

基于页面的导航

Yes, this is possible.是的,这是可能的。 First, define hosting controllers for each of your pages like so首先,像这样为每个页面定义托管控制器

class Page1HostingController: WKHostingController<Page1View> {
    override var body: Page1View {
        self.setTitle("page1")
        return Page1View()
    }
}

class Page2HostingController: WKHostingController<Page2View> {
    override var body: Page2View {
        self.setTitle("page2")
        return Page2View()
    }
}

where Page1View and Page2View are your SwiftUI implementations.其中Page1ViewPage2View是您的 SwiftUI 实现。

Then, add new view controllers to your Interface.storyboard , set their implementation classes to your new controller classes and connect them via a "next page" segue.然后,将新的视图控制器添加到您的Interface.storyboard ,将它们的实现类设置为您的新控制器类并通过“下一页”segue 连接它们。

If you are using the new SwiftUI App Lifecycle and can't access Storyboards (or just need a solution for App Delegate Cycle without using Storyboards), you can use the new Page View style in SwiftUI 2:如果您正在使用新的 SwiftUI App Lifecycle 并且无法访问 Storyboards(或者只需要 App Delegate Cycle 的解决方案而不使用 Storyboards),您可以使用 SwiftUI 2 中的新页面视图样式:

WindowGroup {
   TabView {
     Page1View()
     Page2View()
   }
   .tabViewStyle(PageTabViewStyle())
}

There is NavigationLink button which triggers a navigation presentation when pressed.NavigationLink按钮,按下时会触发导航演示。

var body: some View {
    VStack() {
        NavigationLink(destination: Text("Destination view"), label: {
        Text("Title of the NavigationLink button")
      })
    }
  }

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

相关问题 WatchKit基于页面的导航 - WatchKit Page-based navigation WatchOS 2基于文本/表格的基于页面的点 - WatchOS 2 page-based dots over text/table 基于动态页面的Apple Watch应用程序 - Dynamic page-based Apple Watch app 使用 SwiftUI 在 WatchOS 中实现从主 controller 到基于页面的控制器的导航问题 - Issues implementing navigation from a main controller to page based controllers in WatchOS using SwiftUI 刷新基于页面的应用程序 - Refresh Page-Based Application Landmarks SwiftUI 教程 - 创建 watchOS 应用程序 - 导航损坏 - Landmarks SwiftUI Tutorial - Creating watchOS App - Navigation Broken 如何在代码中向基于页面的导航Apple Watch添加新页面 - How to add a New Page in code to Page-Based Navigation Apple Watch 是否可以在当前基于页面的视图中未显示的另一个WKInterfaceController上更改文本? - Is it possible to change text on another WKInterfaceController that is not currently displayed in page-based view? 使用WatchKit(Watch)的基于页面的界面中的页码? - Page number in Page-Based Interface with WatchKit (Watch)? Apple Watch 上的内置锻炼应用程序如何从主表格行导航到一组基于页面的控制器? - How does the inbuilt Workout app on Apple Watch navigate from main table row to a set of page-based controllers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM