Home > OS >  SwiftUI fullScreenCover no Detail
SwiftUI fullScreenCover no Detail

Time:03-16

I need help. I want the detail view to be displayed at the fullScreenCover, with the code below this does not work. via the RowView a template is created from "dataList". With a NavigationLink it works. With the fullScreenCover this does not work here the data is only displayed statically and not dynamically. Thanks in advance for any advice. I have created a simple example project recreating the issue. This is the code:



import SwiftUI

struct Data: Identifiable  {
    
    var id = UUID()
    var title: String
    var titleSecondScreen: String
}


let dataList = [
    Data(title: "Test1", titleSecondScreen: "Second1"),
    Data(title: "Test2", titleSecondScreen: "Second2"),
    Data(title: "Test3", titleSecondScreen: "Second3"),
    Data(title: "Test4", titleSecondScreen: "Second4"),
    Data(title: "Test5", titleSecondScreen: "Second5"),
]



struct ContentView: View {

    let list: Data
    
    @State private  var fullScreen = false
    @State  var selectedData: Data
    
    
    var body: some View {
        
        ForEach(dataList) { value in
            
            Button {
                fullScreen.toggle()
                selectedData = value
            } label: {
                
                Text(value.title)
                    .padding()
                
            }
        }
        .fullScreenCover(isPresented: $fullScreen) {
            fullScreenView(list: dataList[0])
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView(list: dataList[0], selectedData: dataList[0])
    }
}


struct fullScreenView: View {
    
    let list: Data
    
    @Environment(\.presentationMode) var presentationMode
    
    var body: some View {
        
        
        VStack {
            Text(list.titleSecondScreen)
                .padding()
            
            Button {
                presentationMode.wrappedValue.dismiss()
            } label: {
               Text("Closed FullScreen")
            }
        }
    }
}

struct fullScreenView_Previews: PreviewProvider {
    static var previews: some View {
        fullScreenView(list: dataList[0])
    }
}

here is a Github Link: enter link description here

CodePudding user response:

You can use the fullScreen(item:) version to send the selected item to the FullScreenView.

struct MyData: Identifiable  {
    var id = UUID()
    var title: String
    var titleSecondScreen: String
}

let dataList = [
    MyData(title: "Test1", titleSecondScreen: "Second1"),
    MyData(title: "Test2", titleSecondScreen: "Second2"),
    MyData(title: "Test3", titleSecondScreen: "Second3"),
    MyData(title: "Test4", titleSecondScreen: "Second4"),
    MyData(title: "Test5", titleSecondScreen: "Second5"),
]

struct ContentView: View {
    @State private var selectedData: MyData?
    
    var body: some View {
        ForEach(dataList) { value in
            
            Button {
                selectedData = value
            } label: {
                Text(value.title)
                    .padding()
            }
        }
        .fullScreenCover(item: $selectedData) { item in
            FullScreenView(item: item)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}


struct FullScreenView: View {
    
    let item: MyData
    
    @Environment(\.presentationMode) var presentationMode
    
    var body: some View {
        VStack {
            Text(item.titleSecondScreen)
                .padding()
            Button {
                presentationMode.wrappedValue.dismiss()
            } label: {
               Text("Close FullScreen")
            }
        }
    }
}

I've made a couple of other minor changes:

  1. Use standard Swift capitalization of type names
  2. Avoid using the type Data as it conflicts with the normal Swift Data type
  3. Get rid of the unused list parameter in ContentView
  • Related