admin管理员组文章数量:1357577
I've got a list in sidebar of NavigationSplitView. The view in detail should change by selecting an item. For restoration of view I'm using SceneStorage like this:
import SwiftUI
enum ListType: String {
case all = "all"
case frequently = "frequently"
}
struct MyListItem: Identifiable, Hashable {
let type: ListType
let label: String
let id = UUID()
}
struct SelectionMode: View {
@State private var listItems = [
MyListItem(type: .all, label: "alle Unterrichte"),
MyListItem(type: .frequently, label: "am häufigsten")
]
@SceneStorage("ListType") private var selectedType: ListType?
var body: some View {
NavigationSplitView {
List(listItems, id: \.self.type, selection: $selectedType) { type in
NavigationLink("\(type.label)", value: type)
}
} detail: {
NavigationStack {
SelectedList(viewType: selectedType?.rawValue ?? "all")
}
}
}
This works well, with one exception: The selected item is not highlighted in the list. First, “all” should be highlighted or the selected item (e.g. “frequently”) after the restoration. How can I achieve this?
I've got a list in sidebar of NavigationSplitView. The view in detail should change by selecting an item. For restoration of view I'm using SceneStorage like this:
import SwiftUI
enum ListType: String {
case all = "all"
case frequently = "frequently"
}
struct MyListItem: Identifiable, Hashable {
let type: ListType
let label: String
let id = UUID()
}
struct SelectionMode: View {
@State private var listItems = [
MyListItem(type: .all, label: "alle Unterrichte"),
MyListItem(type: .frequently, label: "am häufigsten")
]
@SceneStorage("ListType") private var selectedType: ListType?
var body: some View {
NavigationSplitView {
List(listItems, id: \.self.type, selection: $selectedType) { type in
NavigationLink("\(type.label)", value: type)
}
} detail: {
NavigationStack {
SelectedList(viewType: selectedType?.rawValue ?? "all")
}
}
}
This works well, with one exception: The selected item is not highlighted in the list. First, “all” should be highlighted or the selected item (e.g. “frequently”) after the restoration. How can I achieve this?
Share Improve this question asked Mar 27 at 21:47 user2836375user2836375 1392 silver badges11 bronze badges1 Answer
Reset to default 1Try this approach using a @State private var selectedItem: MyListItem?
to
do the List selection, as it matches the List display MyListItem
type set with .tag(item)
.
struct ContentView: View {
var body: some View {
SelectionMode()
}
}
enum ListType: String {
case all = "all"
case frequently = "frequently"
}
struct MyListItem: Identifiable, Hashable {
let type: ListType
let label: String
let id = UUID()
}
struct SelectionMode: View {
@State private var listItems = [
MyListItem(type: .all, label: "alle Unterrichte"),
MyListItem(type: .frequently, label: "am häufigsten")
]
// only for scenes (screens), will be lost when App is closed
// @SceneStorage("ListType") private var selectedType: ListType?
// for the whole App, for my testing, keeps the storage after App is closed
@AppStorage("ListType") private var selectedType: ListType?
@State private var selectedItem: MyListItem? // <--- here
var body: some View {
NavigationSplitView {
List(listItems, selection: $selectedItem) { item in
Text(item.label).tag(item) // <--- here
}
} detail: {
NavigationStack {
if let selectedItem = selectedItem {
SelectedList(viewType: selectedItem.type.rawValue)
} else {
Text("Select an item").font(.title)
}
}
}
.onAppear {
if selectedType == nil {
selectedType = .all
}
if let index = listItems.firstIndex(where: {$0.type == selectedType!}) {
selectedItem = listItems[index] // <--- here
}
}
.onChange(of: selectedItem) {
selectedType = selectedItem?.type // <--- here
}
}
}
struct SelectedList: View {
let viewType: String
var body: some View {
Text("Selected type: \(viewType)").font(.title)
}
}
本文标签: swiftuiPreselection of list in NavigationSplitView with SceneStorageStack Overflow
版权声明:本文标题:swiftui - Preselection of list in NavigationSplitView with SceneStorage - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744066546a2585105.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论