SwiftUI Academy · Aula

Passando dados para visualizações de detalhes

Envie o item selecionado para a próxima tela.

Aula 2 de 413 etapas

Passando dados para visualizações de detalhes é uma aula grátis de SwiftUI Academy no CoddyKit. Esta é a aula 2 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de SwiftUI Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de SwiftUI Academy inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

Detail Screens Need Data

A detail screen is only useful if it knows which item to show. So when you push it, you hand the selected piece of data straight into the new view. 📦

Pass Through the Initializer

The cleanest way to send data is the detail view initializer. Store the value in a property, then read it inside the body.

struct DetailView: View {
    let title: String
    var body: some View { Text(title) }
}

Wire It in the Link

In the NavigationLink destination, create the detail view and pass the value in. The chosen item flows directly into the next screen.

NavigationLink("Open") {
    DetailView(title: "Welcome")
}

Passing a Whole Model

You are not limited to strings. Pass an entire struct so the detail screen has every field it needs at once.

struct Book { let title: String; let author: String }
DetailView(book: selectedBook)

Looping With Data

Inside a ForEach you can hand each item to its own link, so every row pushes a detail view filled with that exact element.

ForEach(books) { book in
    NavigationLink(book.title) { DetailView(book: book) }
}

Value Types Copy Safely

Because structs are value types, the detail view gets its own copy. Reading it cannot accidentally change the list behind you.

Show Real Detail

With the model in hand, the detail view can display anything: title, author, description, even computed text built from those fields.

var body: some View {
    VStack {
        Text(book.title).font(.title)
        Text(book.author)
    }
}

Prefer the Newer Pattern

For data-driven links, attach a value and let a navigationDestination decide the view. It keeps your row code short and reusable.

NavigationLink(book.title, value: book)

Match the Type

A navigationDestination handles one data type. SwiftUI looks at the value type you pushed and routes it to the matching destination builder.

.navigationDestination(for: Book.self) { book in
    DetailView(book: book)
}

Keep Detail Views Focused

A detail view should receive its data, not fetch it. Passing data in keeps the screen reusable and easy to preview with sample values.

Watch the Direction

Data flows forward, into the detail. To send changes back to the parent later, you will reach for bindings, which come in a future course.

Quick Check

What is the simplest way to give a detail view the item it should display?

Recap: Passing Data

You pass the chosen item into the detail view through its initializer, often a whole struct, so each screen shows exactly the right content. 🎯

Grátis para começar

Aprenda Swift com um tutor de IA — grátis

Escreva e execute código real no seu navegador, obtenha ajuda instantânea de um tutor de IA 24/7 e continue de onde parou na web ou no app.

Cursos
30
Aulas
120

Perguntas Frequentes

A aula “Passando dados para visualizações de detalhes” é grátis?

Sim — o texto completo de “Passando dados para visualizações de detalhes” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de SwiftUI Academy, atualize para CoddyKit PRO. O curso de SwiftUI Academy inclui 4 aulas no total.

O que vou aprender em “Passando dados para visualizações de detalhes”?

Envie o item selecionado para a próxima tela. Você pratica SwiftUI Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar SwiftUI Academy?

Nenhuma experiência prévia é necessária. SwiftUI Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 2 de 4.

Quanto tempo leva a aula “Passando dados para visualizações de detalhes”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de SwiftUI Academy?

Sim. Cada aula de SwiftUI Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Abrindo telas com NavigationLink
  2. Passando dados para visualizações de detalhes
  3. Barra de ferramentas e título de navegação
  4. Caminhos de navegação programática
← Voltar para SwiftUI Academy