Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

93
Visualizações
Updating a binding value pops back to the parent view in the navigation stack

I am passing a Person binding from the first view to the second view to the third view, when I update the binding value in the third view it pops back to the second view, I understand that SwiftUI updates the views that depend on the state value, but is poping the current view is the expected behavior or I am doing something wrong?

struct Person: Identifiable {
    let id = UUID()
    var name: String
    var numbers = [1, 2]
}
struct FirstView: View {
    @State private var people = [Person(name: "Current Name")]
    var body: some View {
        NavigationView {
            List($people) { $person in
                NavigationLink(destination: SecondView(person: $person)) {
                    Text(person.name)
                }
            }
        }
    }
}
struct SecondView: View {
    @Binding var person: Person
    var body: some View {
        Form {
            NavigationLink(destination: ThirdView(person: $person)) {
                Text("Update Info")
            }
        }
    }
}
struct ThirdView: View {
    @Binding var person: Person
    var body: some View {
        Form {
            Button(action: {
                person.numbers.append(3)
            }) {
                Text("Append a new number")
            }
        }
    }
}

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

SwiftUI works by updating the rendered views to match what you have in your state.

In this case, you first have a list that contains an element called Current Name. Using a NavigationLink you select this item.

You update the name and now that previous element no longer exists, it's been replaced by a new element called New Name.

Since Current Name no longer exists, it also cannot be selected any longer, and the view pops back to the list.

To be able to edit the name without popping back, you'll need to make sure that the item on the list is the same, even if the name has changed. You can do this by using an Identifiable struct instead of a String.

struct Person: Identifiable {
  let id = UUID().uuidString
  var name = "Current Name"
}

struct ParentView: View {
  @State private var people = [Person()]
  var body: some View {
    NavigationView {
      List($people) { $person in
        NavigationLink(destination: ChildView(person: $person)) {
          Text(person.name)
        }
      }
    }
  }
}

struct ChildView: View {
  @Binding var person: Person
  var body: some View {
    Button(action: {
      person.name = "New Name"
    }) {
      Text("Update Name")
    }
  }
}
over 4 years ago · Santiago Trujillo Relatório

0

When navigating twice you need to either use isDetailLink(false) or StackNavigationViewStyle, e.g.

struct FirstView: View {
    @State private var people = [Person(name: "Current Name")]
    var body: some View {
        NavigationView {
            List($people) { $person in
                NavigationLink(destination: SecondView(person: $person)) {
                    Text(person.name)
                }
                .isDetailLink(false) // option 1
            }
        }
        .navigationViewStyle(.stack) // option 2
    }
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda