SwiftUI Academy · Lección

Pasar datos a vistas de detalles

Envíe el elemento seleccionado a la pantalla siguiente.

Lección 2 de 413 pasos

Pasar datos a vistas de detalles es una lección gratuita de SwiftUI Academy en CoddyKit. Esta es la lección 2 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de SwiftUI Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de SwiftUI Academy incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en 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. 🎯

Gratis para empezar

Aprende Swift con un tutor de IA — gratis

Escribe y ejecuta código real en tu navegador, obtén ayuda instantánea de un tutor de IA disponible 24/7 y continúa donde lo dejaste en la web o en la aplicación.

Cursos
30
Lecciones
120

Preguntas frecuentes

¿La lección «Pasar datos a vistas de detalles» es gratis?

Sí — el texto completo de «Pasar datos a vistas de detalles» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de SwiftUI Academy, actualiza a CoddyKit PRO. El curso de SwiftUI Academy incluye 4 lecciones en total.

¿Qué aprenderé en «Pasar datos a vistas de detalles»?

Envíe el elemento seleccionado a la pantalla siguiente. Practicas SwiftUI Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar SwiftUI Academy?

No se requiere experiencia previa. SwiftUI Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 2 de 4.

¿Cuánto tiempo toma la lección «Pasar datos a vistas de detalles»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de SwiftUI Academy?

Sí. Cada lección de SwiftUI Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Navegación a pantallas con NavigationLink
  2. Pasar datos a vistas de detalles
  3. Barra de herramientas y título de navegación
  4. Rutas de navegación programática
← Volver a SwiftUI Academy