skip to Main Content

Why fullScreenCover always take just first index of an array?

This is some example of code:

struct TestView: View {
    @State private var isFullScreen: Bool = false
    var body: some View {
        VStack{
            ForEach(0..<5, id:.self) { number in
                VStack{
                Text("(number)")
                    .background(.red)
                    .foregroundColor(.white)
                    .padding(20)
                    .onTapGesture {
                        isFullScreen.toggle()
                    }
                }
                .fullScreenCover(isPresented: $isFullScreen) {
                    test2View(title: number)
                }
            }
        }
    }
}

This is the code of test2View:

struct test2View: View {
    var title:Int
    var body: some View {
        Text("(title)")
    }
}

Whenever I click on any number it always show just 0, but when I make navigationLink instead of fullScreenCover, it works as expected, but navigationLink isn’t a solution for my problem, I want that to be fullScreenCover.

2

Answers


  1. Chosen as BEST ANSWER

    I found a solution using .fullScreenCover item parameter like this:

    struct TestView: View {
        @State private var isFullScreen: Int? = nil
        var body: some View {
            VStack{
                ForEach(0..<5, id:.self) { number in
                    VStack{
                    Text("(number)")
                        .background(.red)
                        .foregroundColor(.white)
                        .padding(20)
                        .onTapGesture {
                            isFullScreen = number
                        }
                    }
                    .fullScreenCover(item: $isFullScreen) { item in
                        test2View(title: item)
                    }
                }
            }
        }
    }
    

  2. It’s because fullScreenCover is using a single isFullScreen for each number so only the first one works. Fix by adding a third intermediary View to hold an isFullScreen bool for each number, e.g.

    struct TestView: View {
        var body: some View {
            VStack{
                ForEach(0..<5) { number in
                    TestView2(number: number)
                }
            }
        }
    }
    
    struct TestView2: View {
        let number: Int
        @State private var isFullScreen: Bool = false
        
        var body: some View {
            Text("(number, format: .number)")
                .background(.red)
                .foregroundColor(.white)
                .padding(20)
                .onTapGesture {
                    isFullScreen.toggle()
                }
                .fullScreenCover(isPresented: $isFullScreen) {
                    TestView3(number: number)
                }
        }
    }
    
    struct TestView3: View {
        let number: Int
        
        var body: some View {
            Text("(number, format: .number)")
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search