SwiftUI Academy · บทเรียน

เส้นทางการนำทางโดยใช้โค้ด

ควบคุมการนำทางด้วยเส้นทาง navigationDestination

บทเรียน 4 จาก 413 ขั้นตอน

เส้นทางการนำทางโดยใช้โค้ด เป็นบทเรียน SwiftUI Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน SwiftUI Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Navigating From Code

Sometimes a tap is not enough. After saving a form you may want to jump screens yourself. SwiftUI lets you drive the stack from code. ⚙️

A Path You Control

Give NavigationStack a path binding. This array is the live list of screens currently pushed, and editing it changes navigation instantly.

@State private var path = NavigationPath()
NavigationStack(path: $path) { HomeView() }

Push by Appending

Append a value to the path and SwiftUI pushes the matching screen. No tap needed, just change the array.

path.append(book)

Routing the Value

A navigationDestination maps each value type in the path to a view, so appended data knows which screen to become.

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

Pop With Remove

Removing the last element pops a screen, exactly like the back button. You stay in full control of the stack from code.

path.removeLast()

Jump Home Instantly

To return all the way to the root, empty the path. Every pushed screen disappears at once, perfect for a Done button. 🏠

path.removeLast(path.count)

Deep Linking

Because the path is just data, you can build it from a URL or notification, pushing several screens so users land deep inside the app.

NavigationPath Holds Mixed Types

A NavigationPath can store values of different types together, letting one stack route books, users, and settings without a shared enum.

Or Use a Typed Array

If every screen shares one type, a plain typed array works as the path and reads a little more clearly than NavigationPath.

@State private var path: [Book] = []

State Drives the Stack

Navigation becomes state. The visible screens always mirror the path array, so your UI stays predictable and easy to reason about.

Test and Restore

Since the path is plain data, you can save it and restore it, recreating exactly where the user left off when they reopen the app.

Quick Check

What happens when you append a value to a NavigationStack path?

Recap: Programmatic Paths

You bind a path to NavigationStack and push or pop screens by editing that array, turning navigation into predictable, testable state. 🎉

เริ่มต้นได้ฟรี

เรียนรู้ Swift ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
30
บทเรียน
120

คำถามที่พบบ่อย

บทเรียน “เส้นทางการนำทางโดยใช้โค้ด” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “เส้นทางการนำทางโดยใช้โค้ด” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส SwiftUI Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “เส้นทางการนำทางโดยใช้โค้ด”

ควบคุมการนำทางด้วยเส้นทาง navigationDestination คุณปฏิบัติ SwiftUI Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน SwiftUI Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน SwiftUI Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “เส้นทางการนำทางโดยใช้โค้ด” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน SwiftUI Academy นี้ได้ไหม

ได้ บทเรียน SwiftUI Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การเปิดหน้าจอด้วย NavigationLink
  2. การส่งข้อมูลไปยัง View รายละเอียด
  3. แถบเครื่องมือและชื่อการนำทาง
  4. เส้นทางการนำทางโดยใช้โค้ด
← กลับไปที่ SwiftUI Academy