arnoapp
arnoapp

Reputation: 2486

SwiftUI: Dismisses View when Binding of List state changes

I have the following example in SwiftUI:

import SwiftUI

struct DetailView: View {
    var element:Int
    @Binding var favList:[Int]
    
    var body: some View {
        Button(action: {
            if !favList.contains(element){
                favList.append(element)
            }
            else{
                favList.removeAll(where: {$0 == element})
            }
        }){
            HStack {
                Image(systemName: (favList.contains(element)) ? "star.slash" : "star")
                Text((favList.contains(element)) ? "Remove from favorites" : "Add to favorites")
            }
            .frame(maxWidth: 300)
        }
    }
}

struct MainView: View {
    let elements = [1,2,3,4]
    @State var favList:[Int] = []
    
    var body: some View {
        NavigationView {
            List{
                if !favList.isEmpty{
                    Section("Favorits"){
                        ForEach(elements, id: \.self){element in
                            if favList.contains(element){
                                NavigationLink(destination: DetailView(element: element, favList: $favList)) {
                                Text("\(element)")
                                }
                            }
                        }
                    }
                }
                Section("All elements"){
                    ForEach(elements, id: \.self){element in
                        if !favList.contains(element){
                            NavigationLink(destination: DetailView(element: element, favList: $favList)) {
                            Text("\(element)")
                            }
                        }
                    }
                }
                
            }
        }
    }
}

If I change the favList in the DetailView the view gets automatically dismissed. I guess this because the List structure changes.

Am I doing something wrong? Is this the intended behavior? How can I avoid this?

Best regards

Upvotes: 2

Views: 221

Answers (1)

Djallil
Djallil

Reputation: 83

i tried with this, it's the same code just fixing the section header. using the fav button in the view doesn't dismiss the view

import SwiftUI

struct DetailView: View {
    var element:Int
    @Binding var favList:[Int]
    
    var body: some View {
        Button(action: {
            if !favList.contains(element){
                favList.append(element)
            }
            else{
                favList.removeAll(where: {$0 == element})
            }
        }){
            HStack {
                Image(systemName: (favList.contains(element)) ? "star.slash" : "star")
                Text((favList.contains(element)) ? "Remove from favorites" : "Add to favorites")
            }
            .frame(maxWidth: 300)
        }
    }
}

struct MainView: View {
    let elements = [1,2,3,4]
    @State var favList:[Int] = []
    
    var body: some View {
        NavigationView {
            List{
                if !favList.isEmpty{
                    Section(header:Text("Favorits")){
                        ForEach(elements, id: \.self){element in
                            if favList.contains(element){
                                NavigationLink(destination: DetailView(element: element, favList: $favList)) {
                                Text("\(element)")
                                }
                            }
                        }
                    }
                }
                Section(header:Text("Favorits")){
                    ForEach(elements, id: \.self){element in
                        if !favList.contains(element){
                            NavigationLink(destination: DetailView(element: element, favList: $favList)) {
                            Text("\(element)")
                            }
                        }
                    }
                }
            }
        }
    }
}

Tried it on xcode 12,5 with iOS 14 as target

Upvotes: 1

Related Questions