SwiftUI:选项卡式视图中的MasterDetailView会丢失“状态" [英] SwiftUI: MasterDetailView within in Tabbed View loses "State"

查看:54
本文介绍了SwiftUI:选项卡式视图中的MasterDetailView会丢失“状态"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在选项卡式视图中有一个MasterDetailView.如果用户使用Tab键选择MasterDetailView并在主视图中选择一个条目,则详细信息将显示在详细信息视图中.选择另一个选项卡并切换回MasterDetailView之后,不再选择细节-MasterDetailView完全失去其状态,就像完全渲染一样.

I have a MasterDetailView within a Tabbed View. If the user tabs the MasterDetailView and selects an entry in the master view, the detail is presented in the detail view. After selecting another tab and switching back to the MasterDetailView, the detail is no longer selected - the MasterDetailView completely loses its state like is is completely rendered.

private let dateFormatter: DateFormatter = {
    let dateFormatter = DateFormatter()
    dateFormatter.dateStyle = .medium
    dateFormatter.timeStyle = .medium
    return dateFormatter
}()

struct MasterDetailView: View {
    @State private var dates = [Date]()
    var body: some View {
        NavigationView {
            MasterView(dates: $dates)
                .navigationBarTitle(Text("Master"))
                .navigationBarItems(
                    leading: EditButton(),
                    trailing: Button(
                        action: {
                            withAnimation { self.dates.insert(Date(), at: 0) }
                        }
                    ) {
                        Image(systemName: "plus")
                    }
                )
            DetailView()
        }.navigationViewStyle(DoubleColumnNavigationViewStyle())
    }
}

struct MasterView: View {
    @Binding var dates: [Date]

    var body: some View {
        List {
            ForEach(dates, id: \.self) { date in
                NavigationLink(
                    destination: DetailView(selectedDate: date)
                ) {
                    Text("\(date, formatter: dateFormatter)")
                }
            }.onDelete { indices in
                indices.forEach { self.dates.remove(at: $0) }
            }
        }
    }
}

struct DetailView: View {
    var selectedDate: Date?    

    var body: some View {
        Group {
            if selectedDate != nil {
                Text("\(selectedDate!, formatter: dateFormatter)")
            } else {
                Text("Detail view content goes here")
            }
        }.navigationBarTitle(Text("Detail"))
    }
}

struct ContentView: View {      
    @State private var selection = 0
    var body: some View {
        TabView(selection: $selection){
            Text("First View")
                .font(.title)
                .tabItem {
                    VStack {
                        Image("first")
                        Text("First")
                    }
                }
                .tag(0)

            MasterDetailView()
                .tabItem {
                    VStack {
                        Image("second")
                        Text("Master Detail")
                    }
                }
                .tag(1)
        }
    }
}

当用户选择该选项卡时,是否有办法重用" MasterDetailView?

Is there a way to "reuse" the MasterDetailView when the user selects that tab?

我知道我可以使用@State和@Binding保存和还原状态(例如主视图中的选定条目),在这个简单示例中,这可能是一个解决方案.但是在一个复杂的应用程序中(例如,当MasterDetailView包括一个深视图层次结构时),管理(保存和还原)视图的完整状态是没有用的.

I know I can use @State and @Binding to save and restore the state (like the selected entry in the master view) and in that simple example that might be a solution. But in a complex app - for example when the MasterDetailView includes a deep view hierarchy - it's not useful to manage (save and restore) the complete state of a view.

推荐答案

关于此问题的变体已经问了好几次了,到目前为止,共识是SwiftUI还不支持这种用例.我确定这已经在他们的雷达上,但是询问此功能的人越多,它越可能优先考虑明年的更新.

Variations on this question have been asked several times, and so far, the consensus is that SwiftUI does not support this use case yet. I'm sure it's on their radar, but the more people who ask for this feature, the more likely it will be prioritized for next year's updates.

这是一个答案,其中有人通过包装UITabBarController与SwiftUI一起使用来获得您想要的行为.

Here's an answer where someone got the behavior you're wanting by wrapping UITabBarController for use with SwiftUI.

这篇关于SwiftUI:选项卡式视图中的MasterDetailView会丢失“状态"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆