如何使Swiftui的NavigationView记得MacOS中其成员的状态?
我已经在macOS中实现了swiftui navigationView
如下:
struct App1: App {
var body: some Scene {
WindowGroup {
NavigationView {
SideBar()
Text("SCREEN 1")
}
}
}
}
其中sidebar()
是导航链接的列表,如下所示:
struct SideBar: View {
var body: some View {
List {
NavigationLink(destination: SomeListView1()){
Text("Item1")
}
NavigationLink(destination: SomeListView2()){
Text("Item2")
}
NavigationLink(destination: SomeListView3()){
Text("Item3")
}
}
}
}
其中somelistviewn()项目列表。
当我单击侧栏中的不同导航链接时,每次都会重新加载相关视图。例如,如果我要单击item1
,然后滚动到someListView1()
中的项目列表的底部,然后切换到item2
和返回item1
,someListView1()
已重新加载,即,它忘记了其滚动位置并重新确定视图。我希望导航视图记住它们的状态,因此,当您切换到侧边栏中的不同项目并切换后,您可以继续关闭的位置。
如何实现?
I have implemented a SwiftUI NavigationView
in macOS as follows:
struct App1: App {
var body: some Scene {
WindowGroup {
NavigationView {
SideBar()
Text("SCREEN 1")
}
}
}
}
where SideBar()
is a list of navigation links as follows:
struct SideBar: View {
var body: some View {
List {
NavigationLink(destination: SomeListView1()){
Text("Item1")
}
NavigationLink(destination: SomeListView2()){
Text("Item2")
}
NavigationLink(destination: SomeListView3()){
Text("Item3")
}
}
}
}
where SomeListViewn()
contains a list of items.
When I click different navigation links in the side bar, the related views are reloaded each time. For example, if I was to click Item1
and scroll to the bottom of the list of items in SomeListView1()
, then switch to Item2
and back to Item1
, SomeListView1()
is reloaded, i.e., it forgets its scroll position and re-initialises the view. I want the navigation views to remember their state so when you switch to different items in the sidebar and switch back, you can continue where you left off.
How can this be achieved?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论